编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。
+----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+
例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。
+---------------------+ | SecondHighestSalary | +---------------------+ | 200 | +---------------------+
select MAX(Salary) as SecondHighestSalary
from Employee
where Salary<(select MAX(Salary) from Employee)
select
(select distinct salary
from Employee
order by salary desc limit 1,1)
as SecondHighestSalary ;
select
IFNUll((
Select DISTINCT Salary
from Employee
order by Salary desc
LIMIT 1 , 1),null) As SecondHighestSalary
本文介绍了一种SQL查询技巧,用于从Employee表中找出第二高的薪水。通过使用子查询和聚合函数,我们可以有效地找到目标数据,即使在没有第二高薪水的情况下也能正确处理。
353

被折叠的 条评论
为什么被折叠?



