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 |
ORDER BY grade DESCarranges all seven rows from the highest grade.LIMIT 3cuts 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.
OFFSET 0is the same as writing noOFFSET. The number afterOFFSETis how many rows are skipped, so counting starts at 0:OFFSET 10skips 10 rows and starts taking from the 11th row.LIMIT 3 OFFSET 0on a table of 10 rows gives exactly 3 rows.- A page past the end of the data is not an error.
LIMIT 5 OFFSET 10on a table of 7 rows gives 0 rows.
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:
SELECT * FROM students LIMIT 3 ORDER BY grade DESC;is answered withnear "ORDER": syntax error.SELECT * FROM students OFFSET 3;is answered withnear "3": syntax error. SQLite readsOFFSETthere as another name (an alias) for the tablestudents, so the error points at the number3.
💡 SQLite, MySQL and PostgreSQL differ here
LIMIT 3 OFFSET 3works in all three.- MySQL and SQLite also accept
LIMIT 3, 3(with a comma). The first number in this form is the OFFSET, the second is how many rows to take. PostgreSQL refuses this form. The formLIMIT ... OFFSET ...is easier to read. OFFSETwithoutLIMITis allowed in PostgreSQL, but SQLite and MySQL refuse it.
⚠️ Pages that count wrong or keep shifting
- LIMIT without ORDER BY. The result is any rows, not the best or the newest ones.
- Starting OFFSET at 1. Page 1 is
OFFSET 0.OFFSET 1actually skips the first row. - Ties at a page boundary. If the 3rd and 4th rows have the same value, the order of those two rows is not decided. So what pages 1 and 2 show can change from one run to the next. Add a second key to
ORDER BYso the pages stay the same. - A very large OFFSET. With
OFFSET 100000, the database still reads those 100,000 rows first, then throws them away. So pages far from the start open more and more slowly. - Another option for a "load more" button: keyset. Remember the value of the last row on the previous page, then continue from that value. Page 1 ends with Dewi, whose grade is 90, so page 2 is
WHERE grade < 90 ORDER BY grade DESC LIMIT 3: Budi, Rizky, Dimas. With an index on thegradecolumn (see the lesson Indexes & Performance), the database starts at 90 right away, without reading the rows it skips. OFFSET is still the right choice when users need to jump to a certain page, because keyset can only move forward one page at a time. If there are tied values, also remember theidas a second key.
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.