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