SQL Cookbook Lesson

How to limit results in SQL

Learn how to limit results in SQL with examples and explanations from SQLPad.

Problem

You have a database and you want to select a specific number of rows from a table, rather than the entire set of rows.

Sample Data

Consider we have a table called employees with the following data:

id name salary
1 John 3000
2 Mike 4000
3 Sally 5000
4 Alice 6000
5 Bob 7000

MySQL Solution

SELECT * FROM employees LIMIT 3;

Explanation for MySQL Solution

In MySQL, the LIMIT keyword is used to select a limited number of records. The above SQL statement selects the first 3 records from the employees table.

PostgreSQL Solution

SELECT * FROM employees LIMIT 3;

Explanation for PostgreSQL Solution

In PostgreSQL, similar to MySQL, the LIMIT keyword is used to constrain the number of rows returned by the query. The above SQL statement selects the first 3 records from the employees table.

Keep Practicing

Related Cookbook Lessons

View all lessons

Interview Prep

Begin Your SQL, Python, and R Journey

Master 230 interview-style coding questions and build the data skills needed for analyst, scientist, and engineering roles.