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

后端 未结 30 652
醉酒成梦
醉酒成梦 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:07

    Method 1:

    SELECT TOP 1 salary FROM (
    SELECT TOP 3 salary 
     FROM employees 
      ORDER BY salary DESC) AS emp 
     ORDER BY salary ASC
    

    Method 2:

      Select EmpName,salary from
      (
        select EmpName,salary ,Row_Number() over(order by salary desc) as rowid      
         from EmpTbl)
       as a where rowid=3
    

提交回复
热议问题