Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 To find Nth max salary

Author  Topic 

varalaruu
Starting Member

1 Post

Posted - 2008-11-19 : 11:34:47
write the query of to find the n th maximum salary?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-19 : 11:41:40
SELECT Salary
FROM
(SELECT ROW_NUMBER() OVER(ORDER BY Salary) AS Seq,Salary
FROM Table)t
WHERE t.Seq=@N
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-19 : 12:23:45
also see this

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/find-nth-maximum-value.aspx
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2008-11-20 : 01:17:58
Fairly Interview Question

Arnav
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page

karthickbabu
Posting Yak Master

151 Posts

Posted - 2008-11-20 : 05:13:22


Give Nth position instead of 'N'

SELECT * FROM TableName T1 WHERE (N-1) =
(SELECT COUNT(DISTINCT(T2.Salary)) FROM TableName T2 WHERE T2.Salary > T1.Salary)

====================================================
you realize you've made a mistake, take immediate steps to correct it.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-11-20 : 05:19:39
ROW_NUMBER() is probably the most efficient way as Visakh suggested.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -