LIMIT
The SQL LIMIT
operator is used to specify the number of rows we want to fetch. It’s especially useful when we are working with large datasets and want to view a quick preview of the table; rather than loading the entire table into memory, we can use LIMIT
to control the number of rows in our query result set.
Using LIMIT
is super simple: just add one line of code to the end of your query.
For example, if we wanted to fetch only the first 5 rows of our students
table, we could write:
SELECT *
FROM students
LIMIT 5
and only the first 5 rows will appear in our query result set:
Or, we could write:
SELECT
id,
name,
age
FROM students
LIMIT 1
and we’ll get the first row of the students
table (but only the id
, name
and age
columns):
Practice
Now it's your turn! Open the following questions in a new tab to try out the LIMIT
operator: