How to Limit Results in Oracle Database: Oracle Operators: ROWNUM ORDER BY Table of Contents Problem Example Solution Discussion Problem You want to limit the number of rows resulting from a query in Oracle. Example In the exam table, there are names of the students with the results of the exam. nameexam_result Janet Morgen9 Taya Bain11 Anne Johnson11 Josh Kaur10 Ellen Thornton8 You want to get the three rows with the best exam results. Solution SELECT * FROM ( SELECT * FROM exam ORDER BY exam_result DESC ) WHERE ROWNUM <= 3; The result of the query looks like this: nameexam_result Taya Bain11 Anne Johnson11 Josh Kaur10 Discussion In the FROM clause, use a subquery that sorts the rows by the exam_result column in descending order. SELECT * FROM exam ORDER BY exam_result DESC In the main query, write a condition that the number of rows should be less than or equal to 3. In Oracle you can use the pseudocolumn ROWNUM (the row number) to do it. To make sure you'll see only the first three rows, you need a condition that the row number should be less than or equal to 3. WHERE ROWNUM <= 3 You need a subquery just to sort the rows. If you'd like three rows randomly instead of the best three, simply write the table name instead of the subquery. SELECT * FROM exam WHERE ROWNUM <= 3; Watch out! This will not work when you're looking for rows with a number greater than some value (for example, ROWNUM > 2 will not return any rows.) Recommended courses: SQL Basics SQL Practice Set Recommended articles: SQL Basics Cheat Sheet Oracle Top-N Queries for Absolute Beginners How to Sort Records with the ORDER BY Clause How ORDER BY and NULL Work Together in SQL See also: How to Find Rows with Maximum Value How to Order By Two Columns in SQL? How to Filter Records with Aggregate Function COUNT How to Number Rows in SQL Subscribe to our newsletter Join our monthly newsletter to be notified about the latest posts. Email address How Do You Write a SELECT Statement in SQL? What Is a Foreign Key in SQL? Enumerate and Explain All the Basic Elements of an SQL Query