Home > DeveloperSection > Forums > Find nth highest salary in Sql Serever
Ankit Singh

Total Post:341

Posted on    December-09-2015 8:52 PM

 Database SQL Server  SQL  SQL Server 2008  SQL Server 2012 

 1 Reply(s)
 651  View(s)
Rate this:
I want to use Find nth highest salary  in Sql Serever please help me.

aditya kumar Patel

Total Post:254

Posted on    December-09-2015 9:03 PM

We can also use the TOP keyword (for databases that support the TOP keyword, like SQL Server) to find the nth highest salary.

SELECT TOP 1 Emp_ID,Emp_Name,Emp_Sal,Location
      SELECT DISTINCT TOP 1 Emp_ID,Emp_Name,Emp_Sal,Location
      FROM Employee
      ORDER BY Emp_Sal DESC
      ) AS Emp

To understand the query above, first look at the subquery, which simply finds the N highest salaries in the Employee table and arranges them in descending order. Then, the outer query will actually rearrange those values in ascending order, which is what the very last line “ORDER BY Salary” does, because of the fact that the ORDER BY Default is to sort values in ASCENDING order. Finally, that means the Nth highest salary will be at the top of the list of salaries, which means we just want the first row, which is exactly what “SELECT TOP 1 Salary”

Don't want to miss updates? Please click the below button!

Follow MindStick