您的位置:首页 > 其它

Second Highest Salary(选择第二高的工资)

2018-03-09 14:40 239 查看
要求:

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: