Query the second highest salary
Write a SQL query to get the second highest salary in the Employee table (Salary)
id salary
1 100
2 200
3 300
For example, in the above Employee table, the SQL query should return 200 as the second highest salary. If there is no second highest salary, the query should return null.
The results are as follows:
200
Solution:
Another way of thinking, first find out the data except for the first place, and then find the remaining maximum value
SELECT MAX(salary) secondhighestsalary
FROM salary a
WHERE (
SELECT MAX(salary)
FROM salary a
) > salary
;
secondhighestsalary
200
Remarks: create table and data
create table salary(id int, salary int);
insert into salary values(1,100);
insert into salary values(2,200);
insert into salary values(3,300);
insert into salary values(4,200);
insert into salary values(5,300);