How to get second-highest salary employees in a table

后端 未结 30 975
离开以前
离开以前 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 21:07

    Select * from employee where salary = (Select max(salary) from employee where salary not in(Select max(salary)from employee))

    Explanation :

    • Query 1 : Select max(salary) from employee where salary not in(Select max(salary) from employee) - This query will retrieve second highest salary

    • Query 2 : Select * from employee where salary=(Query 1) - This query will retrieve all the records having second highest salary(Second highest salary may have multiple records)

提交回复
热议问题