編寫一個 SQL 查詢,獲取 Employee
表中第二高的薪水(Salary) 。spa
+----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+
例如上述 Employee
表,SQL查詢應該返回 200
做爲第二高的薪水。若是不存在第二高的薪水,那麼查詢應返回 null
。code
+---------------------+ | SecondHighestSalary | +---------------------+ | 200 | +---------------------+
答案1:使用臨時表
SELECT (SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1) AS SecondHighestSalary ;
答案2:使用IFNULL
select ifnull((select distinct salary from employee order by salary desc limit 1,1),null) as SecondHighestSalary ;