RianAcademy
Search
⌘
K
Premium

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
table_fields
The columns to include in the result.
table_name
The table to select data from.
skip_count

How many rows to skip before the output starts. If omitted, the output starts from the first row.

Optional
row_count

How many rows to return. If the result has fewer rows, all of them are returned.

OFFSET syntax:

MySQL 8.4
SELECT table_fields
FROM table_name
LIMIT row_count [OFFSET skip_count];
  • [ ]optional part
table_fields
The columns to include in the result.
table_name
The table to select data from.
row_count

How many rows to return. If the result has fewer rows, all of them are returned.

skip_count

How many rows to skip before the output starts. If omitted, the output starts from the first row.

Optional

Examples

Let's take a Company table:

idname
1Don_avia
2Aeroflot
3Dale_avia
4Air_France
5British_AW

To output lines 3 to 5, you need to use this request:

MySQL 8.4
SELECT * FROM Company LIMIT 2, 3;

Or what's the same thing:

MySQL 8.4
SELECT * FROM Company LIMIT 3 OFFSET 2;

The query returns the following selection:

idname
3Dale_avia
4Air_France
5British_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
Number of skipped records
Number of records to output
idname
11Don_avia
22Aeroflot
33Dale_avia
44Air_France
55British_AW
Select the rows of the table to see how the query is changing ⚡
Tasks for self-testing
0 / 2
1. Restricting entries from the beginning of the table

Sort the list of companies (Company table) by the name in ascending order and display first two records.
Company
id
INT
name
VARCHAR
Passenger
id
INT
name
VARCHAR
Pass_in_trip
id
INT
trip
INT
passenger
INT
place
VARCHAR
Trip
id
INT
company
INT
plane
VARCHAR
town_from
VARCHAR
town_to
VARCHAR
time_out
DATETIME
time_in
DATETIME
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Result
Company
Passenger
Pass_in_trip
Trip

Run a query to see the result, or open a table above