How to find 2nd highest salary simple way in sql server



Finding Nth highest salary in a table is the most common question asked in interviews. I have displayed two ways.. 

1. Using sub Query-

Query: 

Select top 1 Salary From (Select distinct top 3 Salary From Employee order by Salary desc) emp order by Salary 

2. using dense_rank() function-

Query: 

Select Id, Name, Salary From (Select Id, Name, Salary, DENSE_RANK() Over (Order By Salary Desc) salary_rank From Employee) Emp Where salary_rank=2

 

Click here for youtube link

Comments

Post a Comment

Popular posts from this blog

Attribute-based routing in web api

What is rest api