解题思路:求出最大工资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)