C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






find nth highest salary


Posted Date: 26 Apr 2008    Resource Type: Code Snippets    Category: SQL

Posted By: @@@ Hyderabadi Biryani @@@       Member Level: Diamond
Rating:     Points: 2



The following sample code prints out the nth highest salary in a given table.


SELECT TOP 1 salary
FROM (SELECT DISTINCT TOP n salary FROM employee ORDER BY salary DESC) a
ORDER BY salary



The logic followed in the above example is pretty simple. First the nested query gives a single column of list of all distinct salary values in descending order. This table when ordered by salary has the nth at the top, thus explaining TOP 1 clause

The same strategy can be used for finding any nth highest value (or lowest value) by just replacing the "sal" column by the respective column.




Responses

Author: andy robarts    28 Apr 2008Member Level: Gold   Points : 2
select max(sal) from emp order by sal desc;



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sql query for n the highest value in a table  .  Sql query for finding n the highest salary  .  Nth highest salary  .  N th highest value in a table  .  N th highest salary  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Find highest salary among two tables
Previous Resource: Customized sorting order
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

budget conference call

Contact Us    Privacy Policy    Terms Of Use