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;

猜你喜欢

转载自blog.csdn.net/albert48/article/details/83588900