gpt4 book ai didi

sql - 第二高薪水

转载 作者:行者123 更新时间:2023-12-05 08:40:49 27 4
gpt4 key购买 nike

编写SQL查询,从Employee表中获取第二高的薪水。

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

例如,给定上面的 Employee 表,查询应该返回 200 作为第二高的薪水。如果没有第二高的薪水,则查询应返回 null。

    | SecondHighestSalary |
| 200 |

这是Leetcode的一道题,我输入了如下代码:

    SELECT CASE WHEN Salary = '' 
THEN NULL
ELSE Salary
END AS SecondHighestSalary
FROM (SELECT TOP 2 Salary
,ROW_NUMBER() OVER (ORDER BY Salary DESC) AS Num
FROM Employee
ORDER BY Salary DESC) AS T
WHERE T.Num = 2

它表示如果没有第二高薪水的值,查询不会返回 NULL。 例如。如果表格是

   | Id | Salary| 
| 1 | 100 |

查询应该返回

   |SecondHighestSalary|
| null |

而不是

   |SecondHighestSalary|
| |

最佳答案

Leetcode 2nd highest salary 问题的解决方案是:

Select 
Max(Salary) AS SecondHighestSalary
from Employee
where Salary < (
Select Max(Salary) from Employee
);

关于sql - 第二高薪水,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53296941/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com