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

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

    In SQL Server 2012+, OFFSET...FETCH would be an efficient way to achieve this:

    DECLARE @N AS INT;
    SET @N = 3;
    
    SELECT
        EmpSalary
    FROM
        dbo.Salary
    ORDER BY
        EmpSalary DESC
    OFFSET (@N-1) ROWS
    FETCH NEXT 1 ROWS ONLY
    

提交回复
热议问题