admin管理员组

文章数量:1122875

数据库查询

解题思路:

找出第二高的薪水,先找出表里的最大值e2.Salary(300),再找出表里满足(e1.Salary<e2.Salary)的e1.Salary取出(100,200),再通过max()得到e1.Salary(200),即是第二高的薪水

把外层sql的结果,拿到内层sql去测试,如果内层的sql成立,则该行取出。

select max(Salary) as SecondHighestSalary from Employee e1 where 
(select max(Salary) from Employee e2 where e1.Salary<e2.Salary)

另外一种简洁的方法

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

第三种方法

select IFNULL((select Distinct Salary from Employee order by Salary DESC limit 1,1),null) 
as SecondHighestSalary

 

本文标签: 数据库查询