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 from table


Posted Date: 22 May 2008    Resource Type: Code Snippets    Category: SQL

Posted By: Kamal       Member Level: Gold
Rating:     Points: 5



In interview you can faced question like to give solution(query) for find Nth highest salary from given employee table.

TO find out 3rd highest salary from table

--Find 3rd highest salary
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP 3 salary
FROM tblSalary
ORDER BY salary DESC) S
ORDER BY salary

General form to find to Nth highest salary from table

--Find Nth highest salary
SELECT TOP 1 salary FROM (
SELECT DISTINCT TOP N salary FROM tblSalary ORDER BY salary DESC) S
ORDER BY salary

There are many solution to solve to this but above solution is easiest.
Take other possible solution,

SELECT MIN(salary) FROM tblSalary WHERE salary IN
(SELECT DISTINCT TOP 3 salary FROM tblSalary ORDER BY salary DESC)

--or--

SELECT MIN(salary) FROM
(SELECT DISTINCT TOP 3 salary FROM tblSalary ORDER BY salary DESC) S




Responses

Author: Saurabh Rajput    23 May 2008Member Level: Bronze   Points : 2
To Find Fifth Highest Salary

Select MIN(salary) FROM (SELECT DISTINCT TOP 5 salary from tblSalary ORDER BY salary DESC)


Author: Kapil Dhawan    17 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code is going to help lots of guys.
Ton Thanks to you
Regards,
Kapil



Author: Nagamohan kumar P    17 Jun 2008Member Level: Gold   Points : 0
Nice Piece of Code it Really helped me


Author: palla    01 Sep 2008Member Level: Silver   Points : 1
hi,

another way is

select e1.salary from employees e1 where 2=select count(distinct(e2.salary ))from employees e2 where e1.salary>e1.salary)order by e1.salary


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Ways to find nth highest salary in a table  .  

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: How to Drop a Database or Table
Previous Resource: How to format in Sql Server
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

audio conferencing services

Contact Us    Privacy Policy    Terms Of Use