,敢教日月换新天。为有牺牲多壮志

[SQL]LeetCode176. 第二高的薪水 | Second Highest Salary

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9720894.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

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

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

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

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

方法1:使用子查询和LIMIT子句

算法:按降序对不同的工资进行排序,然后利用该LIMIT子句获得第二高的工资。

 

1 SELECT DISTINCT
2     Salary AS SecondHighestSalary
3 FROM
4     Employee
5 ORDER BY Salary DESC
6 LIMIT 1 OFFSET 1

但是,如果没有这样的第二高薪,这个解决方案将被判定为“错误答案”,因为此表中可能只有一条记录。为了解决这个问题,我们可以将其作为临时表。

1 SELECT
2     (SELECT DISTINCT
3             Salary
4         FROM
5             Employee
6         ORDER BY Salary DESC
7         LIMIT 1 OFFSET 1) AS SecondHighestSalary
8 ;


方法2:使用IFNULLLIMIT子句

解决'NULL'问题的另一种方法是使用IFNULL如下功能。

1 SELECT
2     IFNULL(
3       (SELECT DISTINCT Salary
4        FROM Employee
5        ORDER BY Salary DESC
6         LIMIT 1 OFFSET 1),
7     NULL) AS SecondHighestSalary

105ms

1 # Write your MySQL query statement below
2 select (
3   select distinct Salary from Employee order by Salary Desc limit 1 offset 1
4 )as SecondHighestSalary

106ms

1 # Write your MySQL query statement below
2 select max(salary) as SecondHighestSalary
3 from employee
4 where salary < (select max(salary) from employee)

108ms

1 # Write your MySQL query statement below
2 # select distinct Salary SecondHighestSalary from Employee order by Salary DESC limit 1,1
3 select ifNull((select distinct Salary from Employee order by Salary Desc limit 1,1),null) as SecondHighestSalary

110ms

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

 

posted @   为敢技术  阅读(466)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°