Pages

Social Icons

Tuesday 25 November 2014

How to find nth highest salary

In this post I will explain how to get the nth highest salary for an employee in different ways. I have below record present in my employee table.

So first is the quite straight forward query


Now second we go with the sub query so that we can find nth salary as well.


So this query you can use to get the nth salary also, for that you have to change the number 2 from TOP 2 salary

Now third we can get the nth highest salary using co-related query as well.



Note: This query will not work with the duplicate salary.

Now we can use cte (common table expression) also for this.



This query will also not work with the duplicate salary. If you want to use the CTE for duplicate salary as well then you have to use the dense rank instead of Rank.




In the above query if you want to get the nth salary then you can change the number 2 with any number.

Thx,
RS