求第二高工资

解题思路:求出最大工资a,    select top 1 Salary  from Employee where Salary <a  order by Salary desc

值得注意的一个问题是“如果不存在第二高的薪水” 怎么返回null?

--第一次这样写是不能返回null的
select Top 1 Salary  from Employee
where Salary<(select  max(Salary) from Employee ) 
order by Salary desc

正确解法1:(这是sql server 的写法,mysql可使用 IFNULL 和 LIMIT )

--将查询结果作为临时表
select ( 
select Top 1 Salary    from Employee 
where Salary<(select  max(Salary) from Employee ) 
order by Salary desc
) 

正确解法2:

select max(Salary)  
from Employee 
where Salary<(select max(Salary) from Employee)

 

相关文章:

  • 2021-09-26
  • 2022-02-08
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2021-08-03
  • 2021-08-12
  • 2021-12-16
  • 2022-12-23
相关资源
相似解决方案