User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Oracle section within the Web Development category of DaniWeb, a massive community of 403,031 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,872 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Oracle advertiser: Programming Forums
Views: 2073 | Replies: 4 | Solved
Reply
Join Date: Feb 2008
Location: India
Posts: 6
Reputation: iceman29 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
iceman29's Avatar
iceman29 iceman29 is offline Offline
Newbie Poster

Sql query to get max 5 values

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Location: India
Posts: 6
Reputation: iceman29 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
iceman29's Avatar
iceman29 iceman29 is offline Offline
Newbie Poster

Re: Sql query to get max 5 values

  #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 3:44 am. Reason: Keep It Organized - please use [code] tags
Learn to appreciate and appreciate to learn.
Reply With Quote  
Join Date: Jun 2008
Posts: 6
Reputation: sqlstarz is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sqlstarz sqlstarz is offline Offline
Newbie Poster

Re: Sql query to get max 5 values

  #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 3:45 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Join Date: Feb 2008
Location: India
Posts: 6
Reputation: iceman29 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
iceman29's Avatar
iceman29 iceman29 is offline Offline
Newbie Poster

Re: Sql query to get max 5 values

  #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 3:44 am. Reason: Keep It Organized - please use [code] tags
Learn to appreciate and appreciate to learn.
Reply With Quote  
Join Date: Jun 2008
Posts: 6
Reputation: sqlstarz is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sqlstarz sqlstarz is offline Offline
Newbie Poster

Re: Sql query to get max 5 values

  #5  
Jul 12th, 2008
@ iceman

Thanx pal for correcting me
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Oracle Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Oracle Forum

All times are GMT -4. The time now is 10:54 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC