Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 E-mail Us
 Oracle Articles
New Oracle Articles

 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog

 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Oracle Support

 SQL Tuning

 Oracle UNIX
 Oracle Linux
 Remote s
 Remote plans
 Application Server

 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S


 Consulting Staff
 Consulting Prices
 Help Wanted!


 Oracle Posters
 Oracle Books

 Oracle Scripts

Don Burleson Blog 







Oracle row_number analytic function

Oracle Database Tips by Donald BurlesonJuly 28, 2015

Question:  I need to run a SQL query to retrieve the top-10 rows from a large table and I heard that the row_number analytic function can help.  How do you use row_number to get top-n rows, and why is it better or worse than the rank function?

Answer:  Oracle has many ways to service any SQL query, and a top-n query can be satisfied in several ways:

  • Using rownum outside an in-line view
  • Using rank with over
  • Using row_number with over

Here is how to use the row_number function to speed-up top-n queries:

   (select empno, sal row_number() over (order by sal desc) rnk from emp)
where rnk <= 5;

You can also use the rank function:

   (select empno, sal, rank() over (order by sal desc ) rnk from emp)
where rnk <= 5;

The old fashioned ROWNUM also works for top-n queries, but can have poor performance:

   (select ename, sal
      from emp
    order by sal desc)
   rownum < 4;

This use of rownum as a predicate filter can cause performance problems.  Using rownum may change the all_rows optimizer mode for a query to first_rows, causing unexpected sub-optimal execution plans. 

One solution is to always include an all_rows hint when using rownum to perform a top-n query.  See tuning SQL with "rownum".



Oracle Training at Sea
oracle dba poster

Follow us on Twitter 
Oracle performance tuning software 
Oracle Linux poster


Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.