SQL Query to find the Nth Highest Salary.

Option:1

SELECT *
FROM SalaryTable S1
WHERE N-1 = (
SELECT COUNT(DISTINCT(S2.OrderID))
FROM SalaryTable S2
WHERE S2.Salary > S1.Salary)

Ex:

SQL Query to find the 4th Highest Salary:

SELECT *
FROM SalaryTable S1
WHERE 4-1 = (
SELECT COUNT(DISTINCT(S2.Salary))
FROM SalaryTable S2
WHERE S2.Salary > S1.Salary)

Option:2

SELECT * FROM SalaryTable ORDER BY Salary DESC LIMIT N-1, 1

Ex:

SQL Query to find the 4th Highest Salary:

SELECT * FROM SalaryTable ORDER BY Salary DESC LIMIT 4-1, 1

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

Powered by WordPress.com.

Up ↑

%d bloggers like this: