简体   繁体   中英

How to get all employee record bases of second highest salary

I know how to get the second highest salary form database table using by

SELECT MAX(Salary) From Employee WHERE Salary < ( SELECT Max(Salary) FROM Employee);

But I want get all employee records whose salary is second largest salary into tables.

Select * from employee where salary=(
SELECT MAX(Salary) From Employee WHERE Salary < ( SELECT Max(Salary) FROM Employee));

try this. it should work.

select * from Employee where Salary in (SELECT MAX(Salary) From Employee WHERE Salary < ( SELECT Max(Salary) FROM Employee));

Just get all the employees whose salary equals the second highest salary you got:

set @second_top_salary=(
SELECT MAX(salary) FROM Employee 
WHERE salary<( SELECT Max(salary) FROM employee)
);
SELECT * FROM employee WHERE salary=@second_top_salary;

DEMO

使用LIMIT:

SELECT Salary FROM (SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT 2) AS Emp ORDER BY Salary LIMIT 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM