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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to return the second row from an order by quer

Author  Topic 

ranvir_2k
Posting Yak Master

180 Posts

Posted - 2011-02-02 : 12:50:39
Hi all,

I have a query where I return the exam scores of students.

This is the query:

Select * from results order by score desc

This returns all the exam scores starting with the highest first.

How would I write this query so that only the second highest score is returned.
And also for example only the fifth highest score is returned.

Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-02-02 : 13:47:24
select Col1, Col2, col3, ...
from
(select row_number() over (order by score desc) rownum,* from results)
where rownum=5 -- example to get the fifth highest


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2011-02-02 : 14:16:39
[code]select Col1, Col2, col3, ...
from
(
select DENSE_RANK() over (order by score desc) rownum,* from results
)
where rownum=5 [/code]
Go to Top of Page
   

- Advertisement -