How to find 5th highest salary in each department?

Swapnil Nikam :

I have a attach table having 7 records in each department. I need to make a query that finds the 5th highest salary for each department.

enter image description here

GMB :

Use row_number() (if you are running MySQL, this requires version 8.0):

select *
from (
    select 
        t.*, 
        row_number() over(partition by department order by salary desc) rn
    from mytable t
) t
where rn = 5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=171173&siteId=1