leetcode 2--Second Highest Salary

https://leetcode.com/problems/second-highest-salary/description/

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 |
+———————+

select salary from employee group by salary union all (select null as salary) 
order by salary desc limit 1 offset 2;

limit 限制位数
offset从位置开始
union all
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。

猜你喜欢

转载自blog.csdn.net/u011644858/article/details/79176210