2nd Highest Salary in SQL Server
- select MAX(ID) from Employees where ID NOT IN (Select MAX(ID) from Employees)
- select MAX(ID) from Employees where ID < (Select MAX(ID) from Employees )
- select top 1 ID from (select top 2 ID from Employees order by id desc) as tblEmployee order by ID ASC
- Using dense_rank to retrieve 2nd Highetst salary if we have duplicate rows:
- with CTETable
- select ID, Name, DENSE_RANK() over (order by ID desc) as Rnk from Employees
- select Name, ID from CTETable where rnk=2
How useful was this post?
Click on a star to rate it!
Average rating / 5. Vote count:
We are sorry that this post was not useful for you!
Let us improve this post!
Thanks for your feedback!