Sql query to get max 5 values

Thread Solved

Join Date: Feb 2008
Posts: 12
Reputation: iceman29 is an unknown quantity at this point 
Solved Threads: 2
iceman29's Avatar
iceman29 iceman29 is offline Offline
Newbie Poster

Sql query to get max 5 values

 
0
  #1
Jun 5th, 2008
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.
Learn to appreciate and appreciate to learn.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: iceman29 is an unknown quantity at this point 
Solved Threads: 2
iceman29's Avatar
iceman29 iceman29 is offline Offline
Newbie Poster

Re: Sql query to get max 5 values

 
0
  #2
Jun 5th, 2008
Got the soln, as below

  1. SELECT * FROM (SELECT a FROM <tbl> ORDER BY a DESC) WHERE rownum<=5;

Thanks.
Last edited by peter_budo; Jul 13th, 2008 at 4:44 am. Reason: Keep It Organized - please use [code] tags
Learn to appreciate and appreciate to learn.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 7
Reputation: sqlstarz is an unknown quantity at this point 
Solved Threads: 0
sqlstarz sqlstarz is offline Offline
Newbie Poster

Re: Sql query to get max 5 values

 
0
  #3
Jul 4th, 2008
  1. SELECT * FROM <tab> WHERE rownum <=5
  2. ORDER BY <a> desc.

I dont think extra subquery is needed here.
pls correct me if m wrong.
Last edited by peter_budo; Jul 13th, 2008 at 4:45 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: iceman29 is an unknown quantity at this point 
Solved Threads: 2
iceman29's Avatar
iceman29 iceman29 is offline Offline
Newbie Poster

Re: Sql query to get max 5 values

 
0
  #4
Jul 12th, 2008
Yes buddy, the extra sub querry is needed, as in below case :

  1. SELECT * FROM <tab> WHERE rownum <=5
  2. 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 :

  1. 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.
Last edited by peter_budo; Jul 13th, 2008 at 4:44 am. Reason: Keep It Organized - please use [code] tags
Learn to appreciate and appreciate to learn.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 7
Reputation: sqlstarz is an unknown quantity at this point 
Solved Threads: 0
sqlstarz sqlstarz is offline Offline
Newbie Poster

Re: Sql query to get max 5 values

 
0
  #5
Jul 12th, 2008
@ iceman

Thanx pal for correcting me
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: pulsar8700 is an unknown quantity at this point 
Solved Threads: 0
pulsar8700 pulsar8700 is offline Offline
Newbie Poster

Re: Sql query to get max 5 values

 
-1
  #6
Oct 5th, 2009
Excellent stuff mate... Thanks a ton!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC