Pages with LIMIT and OFFSET in SQL
Paging without an ORDER BY returns rows in whatever order the engine likes.
SELECT
id,
name,
price
FROM products
ORDER BY price DESC, id
LIMIT 20 OFFSET 40;
How it works
LIMITcaps the rows returned;OFFSETskips that many first.- Page 3 of 20 is
LIMIT 20 OFFSET 40— offset is (page - 1) * size. - Sort by something unique, or a row can appear on two pages.
Keywords and builtins used here
BYDESCFROMLIMITOFFSETORDERSELECT
The run, in numbers
- Lines
- 7
- Characters to type
- 80
- Tokens
- 19
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 15 seconds.
Step 3 of 3 in Sorting & paging, step 8 of 23 in Language basics.