176. Second Highest Salary#1

Solutin#1

# Write your MySQL query statement below
SELECT MAX(Salary) AS SecondHighestSalary
FROM Employee
WHERE Salary < (SELECT MAX(Salary) FROM Employee)

#Using max() will return a NULL 
#if the value doesn't exist. 
#So there is no need to UNION a NULL. 
#Of course, if the second highest value is guaranteed to exist, using LIMIT 1,1 will be the best answer.

Solution#2

SELECT (SELECT DISTINCT Salary
FROM Employee 
ORDER BY Salary DESC LIMIT 1 OFFSET 1
) AS SecondHighestSalary 
#利用了Limit 1,同時在外圈加一圈選擇滿足了Null的情況
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章