Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

query Expand / Collapse
Author
Message
Posted Wednesday, July 03, 2013 12:21 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 03, 2013 12:59 PM
Points: 7, Visits: 14
what is the query to select nth highest salary of emp table
Post #1470194
Posted Wednesday, July 03, 2013 12:28 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 9:20 AM
Points: 43, Visits: 454
manikanta1207 (7/3/2013)
what is the query to select nth highest salary of emp table


select MAX(Salary) from Employee;
Post #1470195
Posted Wednesday, July 03, 2013 12:34 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:21 PM
Points: 12,744, Visits: 31,062
elee1969 (7/3/2013)
manikanta1207 (7/3/2013)
what is the query to select nth highest salary of emp table


select MAX(Salary) from Employee;


this really sounds like homework....
anyway,
the nth highest , ie the second or third, requires the use if a subquery and row_number, or nested top X queries with opposite ORDER BY commands.

SELECT * FROM (select row_number() over(partition by order by.....) myAlais Where RW = 3


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1470200
Posted Wednesday, July 03, 2013 1:01 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 03, 2013 12:59 PM
Points: 7, Visits: 14
give me an ex please
Post #1470202
Posted Wednesday, July 03, 2013 1:52 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:21 PM
Points: 12,744, Visits: 31,062
manikanta1207 (7/3/2013)
give me an ex please


because this seems a lot like homework, i only offered a partial SQL to help you think about the issue;
this doesn't pass a syntax check but provides a couple of clues as to how to tackle the issue
SELECT * FROM (select row_number() over(partition by order by.....) As RW FROM SOMETABLE ) myAlias Where RW = 3 

if you can show your work, and tell us what isn't working, we could help you understand the concepts behind solving the issue.


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1470213
Posted Wednesday, July 03, 2013 7:23 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:31 AM
Points: 3,590, Visits: 5,097
Uhhh... Isn't this a case of using RANK() or DENSE_RANK(), depending on how ties are to be counted?


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1470280
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse