typestar

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

  1. LIMIT caps the rows returned; OFFSET skips that many first.
  2. Page 3 of 20 is LIMIT 20 OFFSET 40 — offset is (page - 1) * size.
  3. Sort by something unique, or a row can appear on two pages.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Sorting & paging, step 8 of 23 in Language basics.

← Previous Next →