Pages

Showing posts with label order of execution. Show all posts
Showing posts with label order of execution. Show all posts

12 August 2013

SQL- Nth Highest Salary

Q How to find the nth highest salary from a table in SQL Server ?
     Consider below the EmployeeDetails table.


EmployeeDetails Table

Let us target to find 2nd highest salary from above table which is 40000.

We can achieve this in the following two ways:

By using TOP clause


SELECT TOP 1 Salary  FROM  

   (SELECT DISTINCT TOP 2 Salary
       FROM EmployeeDetails 
       ORDER BY Salary DESC) A
      ORDER BY Salary