Second Highest Salary(选择第二高的工资)
2018-03-09 14:40
239 查看
要求:
For example, given the above Employee table, the query should return
the second highest salary. If there is no second highest salary, then the query should return
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)
For example, given the above Employee table, the query should return
200as
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)
相关文章推荐
- SQL,176. Second Highest Salary
- 176. Second Highest Salary
- LeetCode 176. Second Highest Salary
- leetcode 176. Second Highest Salary
- Second Highest Salary
- 第二高工资(Second Higest Salary)
- 【leetcode Database】176. Second Highest Salary
- Leetcode 176. Second Highest Salary
- LeetCode SQL: Second Highest Salary
- Nth Highest Salary --- 找出第N高的工资
- LeetCode Second Highest Salary
- Second Highest Salary
- leetcode176---Second Highest Salary
- 176. Second Highest Salary
- 【LeetCode】175、combine two tables ; 176. Second Highest Salary
- leecode-database 176.Second Highest Salary
- MySQL-Second Highest Salary
- 176. Second Highest Salary
- leetcode 2--Second Highest Salary
- 【leetcode】database Second Highest Salary