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

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

    To query the nth highest bonus, say n=10, using AdventureWorks2012, Try Following code

    USE AdventureWorks2012; 
    GO
    
    SELECT * FROM Sales.SalesPerson;
    GO
    
    DECLARE @grade INT;
    SET @grade = 10;
    SELECT MIN(Bonus)
    FROM (SELECT TOP (@grade) Bonus FROM (SELECT DISTINCT(Bonus) FROM Sales.SalesPerson) AS a ORDER BY Bonus DESC) AS g
    

提交回复
热议问题