176.Second-Highest-Salary
176. Second Highest Salary
题目地址
https://leetcode.com/problems/second-highest-salary/
题目描述
SQL Schema
Write a SQL query to get the second highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+代码
Approach #1 Using sub-query and LIMIT clause
LIMIT clauseHowever, this solution will be judged as 'Wrong Answer' if there is no such second highest salary since there might be only one record in this table. To overcome this issue, we can take this as a temp table.
# Write your MySQL query statement below
SELECT DISTINCT
Salary AS SecondHighestSalary
FROM
Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1
## To overcome this issue, we can take this as a temp table.
SELECT
(SELECT DISTINCT
Salary
FROM
Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1) AS SecondHighestSalary
;Approach #2 Using IFNULL and LIMIT clause
IFNULL and LIMIT clauseAnother way to solve the 'NULL' problem is to use IFNULL funtion as below.
SELECT
IFNULL(
(SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1),
NULL) AS SecondHightestSalaryLast updated
Was this helpful?