12 March 2013

Get nth highest and lowest salary of an employee


One student of me asked "how can we get nth highest and lowest salary on an employee ?". In this article I am going to expose, how can we achieve this in SQL Server.
Suppose we have employee name and salary as shown in below fig.






Query to get nth(3rd) Highest Salary
1.  Select TOP 1 Salary as '3rd Highest Salary' 
2.  from (SELECT DISTINCT TOP 3 Salary from Employee ORDER BY Salary DESC) 
3.  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 


 

Summary
In this article, I explain how
can you get nth highest and lowest salary of an employee. I hope after reading
this article you will be able to do this. I would like to have feedback from my
blog readers. Please post your feedback, question, or comments about this
article

2 comments:

  1. i used this script shiva after read this article its working....
    thanks for the help

    ReplyDelete
  2. could you please help for 2nd highest and 2nd lowest salary details

    ReplyDelete