How to get second-highest salary employees in a table

后端 未结 30 959
离开以前
离开以前 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:53

    Try this to get the respective nth highest salary.

    SELECT
        *
    FROM
        emp e1
    WHERE
        2 = (
            SELECT
                COUNT(salary)
            FROM
                emp e2
            WHERE
                e2.salary >= e1.salary
        )
    

提交回复
热议问题