How to find Nth highest salary in sql server

Query to get nth Highest Salary

Select TOP 1 Salary as ‘N Highest Salary’ from
(
SELECT DISTINCT TOP Salary from Employee ORDER BY Salary DESC
) a
ORDER BY Salary ASC

Replace N with the number.

Ex. table…

To get nth(3rd) Lowest Salary

Select TOP 1 Salary as ‘3rd Highest Salary’ from (SELECT DISTINCT TOP 3 Salary from Employee ORDER BY Salary DESC) a ORDER BY Salary ASC

Query to get nth(3rd) Lowest Salary

  1. Select TOP 1 Salary as ‘3rd Lowest Salary’
  2. from (SELECT DISTINCT TOP 3 Salary from Employee ORDER BY Salary ASC)
  3. a ORDER BY Salary DESC

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s