0%

Leetcode 176 Second Highest Salary

Write a SQL query to get the second highest salary from the Employee table.

1
2
3
4
5
6
7
+----+--------+
| 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.

1
2
3
4
5
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+

1 LIMIT OFFSET

第一次尝试以失败告终, 原因是因为不知道怎么处理null的情况. 当表中只有1行时, SELECT只会返回empty set 而不是 NULL.

1
2
# error when occur the table contains only one row.
SELECT Salary AS SecondHighestSalary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1;

2 ISNULL()函数

ISNULL(A, B) 当A为NULL的时候返回B. 这样就解决了返回NULL的情况了. 注意, 为了避免并列第一, 要用DISTINCT关键字

1
2
# Write your MySQL query statement below
SELECT IFNULL((SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1), NULL) AS SecondHighestSalary;