1. 程式人生 > >sql leetcode 176. Second Highest Salary

sql leetcode 176. Second Highest Salary

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

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

For example, given the above Employee table, the query should return 200as the second highest salary. If there is no second highest salary, then the query should return null

.

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

LIMIT a OFFSET b 返回的是從第a行開始的第b個數據

注意:第一個被檢索的行是第0行,而不是第一行。LIMIT 1 OFFSET 1 會檢索第二行

可以簡寫為LIMIT a,b

SELECT (SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC
       LIMIT 1 OFFSET 1) AS SecondHighestSalary;