MYSQL query the second-highest salary

Write a SQL query to get the Employee table in the second-highest salary (Salary).

+ -------- + ---- +
| Id | the Salary |
+ ---- + -------- +
|. 1 | 100 |
| 2 | 200 is |
|. 3 | 300 |
+ ---- + -------- +
such as the Employee table above, SQL queries should return 200 as the second-highest salary. If the second-highest salary does not exist, then the query should return null.

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

Source: stay button (LeetCode)

select max(Salary) SecondHighestSalary
from employee
where
salary<(select max(salary) from employee)
 
 
 
select (select distinct salary from Employee order by salary desc limit 1,1) as SecondHighestSalary 

Guess you like

Origin www.cnblogs.com/corvus/p/12019686.html