求第二高工資

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章