美文网首页
leetcode的SQL题(二)

leetcode的SQL题(二)

作者: 瓜皮小咸鱼 | 来源:发表于2019-04-03 09:36 被阅读0次

编写一个 SQL 查询,获取 employee 表中第二高的薪水(Salary)。

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

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

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

题目分析:
获取第二高:即降序排序后然后偏移一位。

答案:

select (
select distinct Salary 
from employee 
order by Salary desc 
limit 1 offset 1)
as SecondHighestSalary ;

相关文章

网友评论

      本文标题:leetcode的SQL题(二)

      本文链接:https://www.haomeiwen.com/subject/qcxabqtx.html