ORACLE: Limit Returned records with rownum

person Jason Huangfolder_openCode, Oracle, SQLlocal_offer, , , , access_time June 22, 2016

The following query will get the latest entered_on date from the result table.


-- This select will get the latest result entered_on date
-- to check after a refresh what's the latest data
-- SELECT * FROM ( your_query_here ) WHERE ROWNUM <= N.
select * from (select entered_on from result where trim(entered_on) is not null order by entered_on desc) where rownum <= 25

the reason to use an inline query is that inline query is getting all the results back, while the main select would grab the top N results using rownum.

If you do “select entered_on from result where rownum <= N order by entered_on desc “, it would return the wrong result, instead of getting the top N records, you would get N random records.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>