DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Oracle (http://www.daniweb.com/forums/forum129.html)
-   -   Sql query to get max 5 values (http://www.daniweb.com/forums/thread127846.html)

iceman29 Jun 5th, 2008 5:08 am
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 Jun 5th, 2008 5:56 am
Re: Sql query to get max 5 values
 
Got the soln, as below

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

Thanks.

sqlstarz Jul 4th, 2008 7:22 pm
Re: Sql query to get max 5 values
 
select * from <tab> where rownum <=5
order by <a> desc.

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

iceman29 Jul 12th, 2008 1:53 am
Re: Sql query to get max 5 values
 
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.

sqlstarz Jul 12th, 2008 2:49 pm
Re: Sql query to get max 5 values
 
@ iceman

Thanx pal for correcting me

pulsar8700 Oct 5th, 2009 4:28 am
Re: Sql query to get max 5 values
 
Excellent stuff mate... Thanks a ton!


All times are GMT -4. The time now is 2:53 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC