| | |
Sql query to get max 5 values
Please support our Oracle advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Got the soln, as below
Thanks.
sql Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Solved Threads: 0
sql Syntax (Toggle Plain Text)
SELECT * FROM <tab> WHERE rownum <=5 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
Yes buddy, the extra sub querry is needed, as in below case :
the first 5 rown are getting sorted and then ordered, which would not essesntaily return max 5 values.
However if we use it as :
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.
sql Syntax (Toggle Plain Text)
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 :
sql Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Help with SQL date evaluation (Oracle)
- query get upcoming birthdays (PHP)
- retrieving a particular value with a sql query (PHP)
- php table help (PHP)
- PHP/SQL query help (PHP)
- using asp array to store db information (ASP)
Other Threads in the Oracle Forum
- Previous Thread: DISTINCT on One Column Only
- Next Thread: Best Oracle Books
| Thread Tools | Search this Thread |
2009predictions acquisition amazon.com bartz bernanke cia citrix cloudcomputing crm database dell economy editor enterprise enterprise2.0 enterprisesoftware federalreserve forbes hp ibm intellipedia internet larryellison layoffs linux loughridge mediawiki michaeljackson microsoft neverland nortel notebooks oil operatingsystem oracle palm rimm saas salesforce sap seagate socialcomputing sql sun sybase technologystocks virtualiron virtualization vmware wiki wikipedia xen yahoo zoho





