Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Limiting number of rows returned by query
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Limiting number of rows returned by query

Karen Morton EXPERT RESPONSE FROM: Karen Morton

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 16 June 2003
In SQL Server, there are a couple of commands (SET ROWCOUNT or TOP) that limit the number of rows that will be returned by a certain query. Is there a similar command in Oracle?

>

You can use ROWNUM and you can also do a TOP-n type query. Here are some examples:

If you want to return only a limited number of rows that are not in any kind of sorted order (this example returns just the first 10 selected rows):

SELECT * 
  FROM my_table
 WHERE ROWNUM < 11 ;
 
If you want to return employee records for employees with the top five salaries:
SELECT e.*
  FROM (SELECT * FROM emp ORDER BY sal) e
 WHERE ROWNUM < 5 ;
The reason you have to use the second format to retrieve the top five is that when Oracle applies a row number (rownum) to rows as they are determined to satisify the query and the order by happens after that. If you simply ask for 'where rownum < 11' as in the first example, you would pull back the rows assigned row numbers 1 - 11... not those rows whose sorted order was 1 - 11.

For More Information


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts