How to find third or nᵗʰ maximum salary from salary table?

后端 未结 30 643
醉酒成梦
醉酒成梦 2020-11-30 16:30

How to find third or nth maximum salary from salary table(EmpID, EmpName, EmpSalary) in optimized way?

30条回答
  •  無奈伤痛
    2020-11-30 17:23

    --nth highest salary

    select * 
    from (select lstName, salary, row_number() over( order by salary desc) as rn 
          from employee) tmp
    where rn = 2
    

    --(nth -1) highest salary

    select * 
    from employee e1
    where 1 = (select count(distinct salary)  
               from employee e2
               where e2.Salary > e1.Salary )
    

提交回复
热议问题