I want to find all the records with the second highest salary in the table. There are many such employees, how do I do that?
Table: Employee
ID salary emp_name emp_address
1 400 A abc
2 800 B def
3 300 C hjs
4 400 D teuu
5 400 E kakn
6 400 E kssj
You can use the dense_rank
as follows:
Select * from
(Select t.*,
dense_rank() over (order by salary desc) rn
From your_table t) t
Where rn = 2
If you are on lower version of mysql in which windows functions are not allowed then you can use corelated query as follows:
Select t.*
From your_table t
Where 1 = (select count(distinct tt.salary)
From your_table tt
Where tt.salary > t.salary)
You might find it simple enough to do:
select t.*
from t
where t.salary = (select distinct t2.salary
from t t2
order by t2.salary desc
limit 1 offset 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.