1. 程式人生 > >leetcode 第二高薪水

leetcode 第二高薪水

problem

直接查詢如果不存在第二高的Salary不會返回值,所以應該用子查詢

select (select distinct Salary from Employee order by Salary desc limit 1,1) as SecondHighestSalary

也可以用IFNULL
ifnull(exr1,exr2) 如果exr1是null那麼返回exr2否則返回exr1

select ifnull( (select distinct Salary from Employee order by Salary desc limit 1 offset 1), NULL) as SecondHighestSalary
select max(Salary) as SecondHighestSalary from Employee where Salary <> (select max(Salary) from Employee)