943,985 Members | Top Members by Rank

Ad:
  • Oracle Discussion Thread
  • Marked Solved
  • Views: 11827
  • Oracle RSS
Jun 5th, 2008
0

Sql query to get max 5 values

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Newbie Poster
iceman29 is offline Offline
12 posts
since Feb 2008
Jun 5th, 2008
0

Re: Sql query to get max 5 values

Got the soln, as below

sql Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 2
Newbie Poster
iceman29 is offline Offline
12 posts
since Feb 2008
Jul 4th, 2008
0

Re: Sql query to get max 5 values

sql Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sqlstarz is offline Offline
7 posts
since Jun 2008
Jul 12th, 2008
0

Re: Sql query to get max 5 values

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

sql Syntax (Toggle Plain Text)
  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 :

sql Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 2
Newbie Poster
iceman29 is offline Offline
12 posts
since Feb 2008
Jul 12th, 2008
0

Re: Sql query to get max 5 values

@ iceman

Thanx pal for correcting me
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sqlstarz is offline Offline
7 posts
since Jun 2008
Oct 5th, 2009
-1

Re: Sql query to get max 5 values

Excellent stuff mate... Thanks a ton!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pulsar8700 is offline Offline
1 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Oracle Forum Timeline: removing first blank line in csv file
Next Thread in Oracle Forum Timeline: Best Oracle Books





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC