954,123 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sql query to get max 5 values

Hi ,

Can we have an sql query to get the max 5 values from a column?
Please paste a sample code if its possible.

Thanks in advance.

iceman29
Newbie Poster
12 posts since Feb 2008
Reputation Points: 10
Solved Threads: 2
 

Got the soln, as below

select * from (select a from <tbl> order by a DESC) where rownum<=5;


Thanks.

iceman29
Newbie Poster
12 posts since Feb 2008
Reputation Points: 10
Solved Threads: 2
 
select * from <tab> where rownum <=5
order by <a> desc.


I dont think extra subquery is needed here.
pls correct me if m wrong.

sqlstarz
Newbie Poster
7 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Yes buddy, the extra sub querry is needed, as in below case :

select * from <tab> where rownum <=5
order by <a> desc


the first 5 rown are getting sorted and then ordered, which would not essesntaily return max 5 values.

However if we use it as :

select * from (select a from <tbl> order by a DESC) where rownum<=5;


the results are first ordered in desceding order i.e max to lowest and on selecting the frist 5 rows, we get the 5 max values.

iceman29
Newbie Poster
12 posts since Feb 2008
Reputation Points: 10
Solved Threads: 2
 

@ iceman

Thanx pal for correcting me

sqlstarz
Newbie Poster
7 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Excellent stuff mate... Thanks a ton!

pulsar8700
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You