Skip to content

Latest commit

 

History

History
67 lines (51 loc) · 1.13 KB

File metadata and controls

67 lines (51 loc) · 1.13 KB

176. Second Highest Salary

难度: Easy

刷题内容

原题连接

内容描述

Write a SQL query to get the second highest salary from the Employee table.

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
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.

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

解题方案

思路 1

beats 62.26%

select Salary SecondHighestSalary from Employee
union
select null
order by SecondHighestSalary 
desc limit 1, 1

思路 2

beats 18.47%

select (
  select distinct Salary from Employee 
  order by Salary 
  desc limit 1, 1
)as SecondHighestSalary

思路 3

beats 52.21%

select (
  select Salary from Employee 
  group by Salary 
  order by Salary 
  desc limit 1,1
)as SecondHighestSalary