Limiting Results — SQL

LIMIT n takes the first n rows of a query's result, and OFFSET m skips m rows first. Always sort first with ORDER BY. Otherwise LIMIT 3 gives any 3 rows, not…

You want the three students with the highest grades, so you write SELECT * FROM students LIMIT 3;. Three rows come back, with no error. But Citra, who has the highest grade, is not in the result.

LIMIT does keep only the first few rows of a query's result. The question is: first in what order? This lesson answers it, and also explains OFFSET, the partner of LIMIT. OFFSET skips a number of rows first, then starts taking rows. Both work behind the search results of an online shop. There are thousands of items, but one page shows only 10. Page 1 takes the first 10 items. Page 2 skips 10 items, then takes the next 10. In SQL, page 2 is LIMIT 10 and OFFSET 10.

LIMIT: take the first N rows

The students table in the Lab and the exercise holds 7 students:

id name grade city
1 Dewi 90 London
2 Rizky 85 Manchester
3 Maya 92 Leeds
4 Andi 78 London
5 Budi 88 Manchester
6 Citra 95 Bristol
7 Dimas 82 York

The query below fetches the three students with the highest grades:

SELECT * FROM students
ORDER BY grade DESC
LIMIT 3;
id name grade city
6 Citra 95 Bristol
3 Maya 92 Leeds
1 Dewi 90 London
  1. ORDER BY grade DESC arranges all seven rows from the highest grade.
  2. LIMIT 3 cuts that sorted result, so only the top 3 rows are left.

So sort first, then cut.

LIMIT without ORDER BY

The query below is the same, but without ORDER BY:

SELECT * FROM students LIMIT 3;

The result is Dewi, Rizky, Maya: the first 3 rows in the order they were stored. Without ORDER BY, LIMIT 3 only means "any 3 rows", not the best 3 students. Citra (95) is in the 6th row, so she is not in it at all.

OFFSET: skip first, then take

OFFSET skips a number of rows at the start of the sorted result. This is the result of ORDER BY grade DESC, split into pages of 3 students:

Page Query Result
1 LIMIT 3 OFFSET 0 Citra, Maya, Dewi
2 LIMIT 3 OFFSET 3 Budi, Rizky, Dimas
3 LIMIT 3 OFFSET 6 Andi

The formula: OFFSET = (page - 1) × LIMIT. For page 3: (3 - 1) × 3 = 6, so 6 rows are skipped. The last page may hold fewer than 3 rows: here only Andi is left.

Clause order

Each part of a query that starts with a command word, such as WHERE or ORDER BY, is called a clause. The clauses are written in a fixed order: SELECT, FROM, WHERE, ORDER BY, LIMIT, OFFSET. If you swap them, SQLite refuses the query. SQLite also refuses an OFFSET written without a LIMIT:

💡 SQLite, MySQL and PostgreSQL differ here

⚠️ Pages that count wrong or keep shifting

TL;DR: LIMIT n takes the first n rows of a query's result, and OFFSET m skips m rows first. Always sort first with ORDER BY. Otherwise LIMIT 3 gives any 3 rows, not the 3 highest values. To split a result into pages, write LIMIT size OFFSET (page - 1) × size, where size is the number of rows per page. Page 1 is OFFSET 0. The parts of a query (clauses) are written in this order: WHERE, ORDER BY, LIMIT, OFFSET.

⚡ Sort first, then cut

Write:

SELECT * FROM students
ORDER BY grade DESC
LIMIT 3;

Meaning: The students table holds 7 students. ORDER BY grade DESC arranges all seven rows from the highest grade. Then LIMIT 3 cuts the result to the top 3 rows: Citra, Maya, Dewi.

⚡ LIMIT without ORDER BY

Guess first: Citra, who has the highest grade, is stored in the 6th row of the table. If the query is SELECT * FROM students LIMIT 3;, with no ORDER BY, is Citra in the result?

No. Without ORDER BY, you only get the first 3 rows in the order they were stored: Dewi, Rizky, Maya.

Meaning: Without ORDER BY, LIMIT 3 gives any 3 rows, not the 3 highest values.

⚡ Pages with OFFSET

Write:

SELECT * FROM students
ORDER BY grade DESC
LIMIT 3 OFFSET 3;

Meaning: OFFSET 3 skips 3 rows first, then LIMIT 3 takes the next 3 rows. If the result is split into pages of 3 students, this query fetches page 2: Budi, Rizky, Dimas. The formula is OFFSET = (page - 1) × LIMIT, so page 1 is OFFSET 0.

⚡ Clause order

Meaning: Each part of a query that starts with a command word, such as WHERE or LIMIT, is called a clause. The clauses are always written in this order: WHERE, ORDER BY, LIMIT, then OFFSET. If LIMIT is written before ORDER BY, SQLite refuses the query with a syntax error.

What you will learn