Second Highest Salary(選擇第二高的工資)
阿新 • • 發佈:2019-01-02
要求:
For example, given the above Employee table, the query should return 200
as
the second highest salary. If there is no second highest salary, then the query should return null
.
Create table If Not Exists Employee (Id int, Salary int); Truncate table Employee; insert into Employee (Id, Salary) values ('1', '100'); insert into Employee (Id, Salary) values ('2', '200'); insert into Employee (Id, Salary) values ('3', '300');
方法一:
select IF(
(select count(DISTINCT Salary) from Employee)>1
,(select Salary from Employee order by Salary desc limit 1,1)
,null)
as SecondHighestSalary;
方法二:
SELECT max(Salary)
FROM Employee
WHERE Salary < (SELECT max(Salary) FROM Employee)