Sub topic 9
Building Paged Queries
This chapter assumes you know basic SELECT and FROM syntax, can filter with WHERE, and can sort rows with ORDER BY. Pagination means returning a large result set in smaller pages. A client can request one page at a time instead of loading every row at once.
The page formula
LIMIT sets the maximum number of rows returned. OFFSET tells the database how many matching rows to skip first.
For a one-based page number and a fixed page size:
OFFSET = (page number - 1) * page sizeLIMIT = page size
For example, page 3 with 10 rows per page skips the first 20 rows and returns the next 10:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 10 OFFSET 20;The first page uses OFFSET 0. The second uses OFFSET 10. The fourth uses OFFSET 30.
| Page | Page size | OFFSET | Returned positions |
|---|---|---|---|
| 1 | 10 | 0 | 1–10 |
| 2 | 10 | 10 | 11–20 |
| 3 | 10 | 20 | 21–30 |
The database applies the sort order before it skips and limits rows. Therefore, the same ordering must be used for every page.
Why ordering must be deterministic
A query without ORDER BY does not promise a row order. Even with ORDER BY, ties can make page boundaries unstable. If several orders have the same order_date, the database may return those tied rows in different orders between requests.
Add a unique tie-breaker to the sort:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 10 OFFSET 20;Here, newer dates appear first. When dates match, the larger order_id appears first. Assuming order_id is unique, every row has one precise position in the result.
How pages can change
LIMIT and OFFSET describe positions at query time. They do not create a permanent snapshot. If a new order is inserted between two requests, later rows may shift forward. A row from the previous page can then appear again, while another row is skipped.
This is especially noticeable when sorting by a value that changes, such as a status or update timestamp. For reports that require a consistent view, use a transaction or another snapshot strategy supported by your database. For ordinary user interfaces, a deterministic order and short delays between requests are often sufficient.
The application should validate page inputs before constructing a query. A page number of zero or a negative page size is invalid. Use parameters for values accepted by the database driver, or safely convert validated numeric inputs to integers when the driver does not parameterize these clauses.
Pagination flow
The database conceptually sorts all matching rows, discards the first 20, and returns at most 10. This explains both the result and the cost: a later page usually requires more rows to be considered before the requested rows are produced.
Exercise
Write a query for page 4 of products, using 25 rows per page. Sort by price from highest to lowest and use product_id as a descending tie-breaker.
Show answer
SELECT productid, price FROM products ORDER BY price DESC, productid DESC LIMIT 25 OFFSET 75;