Selection restriction, LIMIT operator

The LIMIT operator allows you to extract a specific a range of records from one or more tables.

General query structure with the LIMIT operator

MySQL
SELECT table_fields
FROM table_list
LIMIT [number_of_missed_records,] number_of_records_to_output;

If you do not specify the number of skipped records, they will be counted from the beginning of the table.

The LIMIT operator is not implemented in all DBMS, e.g. MSSQL to output entries from the beginning of the table is used the TOP operator, but for those occasions when you need to make the offset from beginning of table, designed design OFFSET FETCH.

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
SELECT * FROM Company LIMIT 2, 3;

Or what's the same thing:

MySQL
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).

Tasks for self-testing

Multi-table queries
Nested SQL queries