LESSON 13 / 62 · Selection limitSelection limit, operator LIMIT
Description and syntax of the SQL limit statement, usage examples, and tasks for self-checking.
Selection restriction, LIMIT operator
The LIMIT operator allows you to extract a specific range of records from one or more tables.
General query structure with the LIMIT operator
Comma syntax:
MySQL 8.4
SELECT table_fields
FROM table_name
LIMIT [skip_count,] row_count;- [ ]optional part
How many rows to skip before the output starts. If omitted, the output starts from the first row.
How many rows to return. If the result has fewer rows, all of them are returned.
- table_fields
- The columns to include in the result.
- table_name
- The table to select data from.
- skip_count
- Optional
OFFSET syntax:
MySQL 8.4
SELECT table_fields
FROM table_name
LIMIT row_count [OFFSET skip_count];- [ ]optional part
How many rows to return. If the result has fewer rows, all of them are returned.
How many rows to skip before the output starts. If omitted, the output starts from the first row.
- table_fields
- The columns to include in the result.
- table_name
- The table to select data from.
- skip_count
- Optional
Examples
Let's take a Company table:
| id | name |
|---|---|
| 1 | Don_avia |
| 2 | Aeroflot |
| 3 | Dale_avia |
| 4 | Air_France |
| 5 | British_AW |
To output lines 3 to 5, you need to use this request:
SELECT * FROM Company LIMIT 2, 3;Or what's the same thing:
SELECT * FROM Company LIMIT 3 OFFSET 2;The query returns the following selection:
| id | name |
|---|---|
| 3 | Dale_avia |
| 4 | Air_France |
| 5 | British_AW |
This query skips the first two rows of the table (1, 2), and then outputs the next three entries (3, 4, 5).
Now try it yourself ⚡️
MySQL 8.4
SELECT * FROM Company
LIMIT 3 OFFSET 2| id | name | |
|---|---|---|
| 1 | 1 | Don_avia |
| 2 | 2 | Aeroflot |
| 3 | 3 | Dale_avia |
| 4 | 4 | Air_France |
| 5 | 5 | British_AW |
Practice
Solve it in the editor. The query runs against a sample database in your browser.
Write a query and run it to see the results.
Write a query and run it to see the results.