How to Limit Results in MySQL, PostgreSQL and SQLite Database: MySQL PostgreSQL SQLite Operators: ORDER BY LIMIT Table of Contents Problem: Example: Solution: Discussion: Problem: You want to limit the number of rows resulting from a query in MySQL, PostgreSQL, or SQLite. 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 exam ORDER BY exam_result DESC LIMIT 3; The result of the query looks like this: nameexam_result Taya Bain11 Anne Johnson11 Josh Kaur10 Discussion: First, sort the rows by the exam_result column in descending order using the ORDER BY clause and the DESC keyword. Then, after the ORDER BY clause, use the LIMIT keyword with a number of rows you'd like to return (here, 3). ORDER BY exam_result DESC LIMIT 3 If you'd like to retrieve three rows randomly instead of the best three, skip the ORDER BY part. SELECT * FROM exam LIMIT 3; Of course, you can retrieve any number of rows you want. Just replace 3 with your desired number. Recommended courses: SQL Basics SQL Basics in PostgreSQL SQL Basics in MySQL SQL Practice Set Recommended articles: PostgreSQL Cheat Sheet SQL Basics Cheat Sheet How to Rank Rows in SQL: A Complete Guide How to Sort Records with the ORDER BY Clause How ORDER BY and NULL Work Together in SQL 19 PostgreSQL Practice Exercises with Detailed Solutions Best Books for Learning PostgreSQL PostgreSQL Date Functions 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