How to get second-highest salary employees in a table

后端 未结 30 950
离开以前
离开以前 2020-12-23 20:15

It\'s a question I got this afternoon:

There a table contains ID, Name, and Salary of Employees, get names of the second-highest salary employees, in SQL Server

30条回答
  •  無奈伤痛
    2020-12-23 20:52

    This might help you

    SELECT 
          MIN(SALARY) 
    FROM 
          EMP 
    WHERE 
          SALARY in (SELECT 
                          DISTINCT TOP 2 SALARY 
                     FROM 
                          EMP 
                     ORDER BY 
                          SALARY DESC
                    )
    

    We can find any nth highest salary by putting n (where n > 0) in place of 2

    Example for 5th highest salary we put n = 5

提交回复
热议问题