Sub topic 8
Skipping Rows with OFFSET
This chapter assumes that you know basic SELECT and FROM syntax, can filter rows with WHERE, and can sort results with ORDER BY. You will use those skills to skip a predictable number of rows and return the next group.
What OFFSET Does
OFFSET tells the database how many rows to skip before returning results. It is usually paired with LIMIT:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 10 OFFSET 20;This query sorts all matching orders, skips the first 20 rows, and returns the next 10. Because counting starts at zero, this represents page 3 when each page contains 10 rows.
| Page | Page size | OFFSET |
|---|---|---|
| 1 | 10 | 0 |
| 2 | 10 | 10 |
| 3 | 10 | 20 |
OFFSET 0 means that no rows are skipped. Some database systems also allow LIMIT 10 without an OFFSET; that has the same effect as OFFSET 0.
Why ORDER BY Matters
An offset has meaning only relative to an ordering. Without ORDER BY, the database is free to return rows in any order. The database might use a different access path after an index change, an update, or a new execution plan.
SELECT order_id, order_date, total
FROM orders
LIMIT 10 OFFSET 20;This query does not reliably mean “the third page of orders.” It means “return 10 rows after some unspecified 20 rows.”
Use a deterministic ordering instead. A deterministic order always gives each row a stable position. If order_date is not unique, add a unique or nearly unique column such as order_id as a tie-breaker.
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 5 OFFSET 10;The database first orders by the newest date. When two orders have the same date, it orders those rows by descending order_id.
Building Page-by-Page Queries
For a page size of page_size, the offset for page page_number is:
(page_number - 1) * page_size
For example, page 4 with 25 rows per page uses OFFSET 75.
SELECT order_id, order_date, total
FROM orders
WHERE customer_id = 42
ORDER BY order_date DESC, order_id DESC
LIMIT 25 OFFSET 75;The WHERE filter is applied to the target set before the query returns the requested page. Keep the same filter, ordering, and page size for every request.
Logical Query Order
Although SQL writes LIMIT and OFFSET at the end, the useful mental model is:
- Find rows from
FROM. - Filter them with
WHERE. - Sort them with
ORDER BY. - Skip rows with
OFFSET. - Return at most the requested number with
LIMIT.
This explains why changing the ordering changes which rows an offset selects.
Performance and Alternatives
For a small offset, this approach is simple and practical. For a large offset, the database generally still has to find and process the skipped rows before it can return the requested page. A request for page 10,000 can therefore become slow.
Keyset pagination avoids counting from the beginning. It remembers the last sort key from the previous page:
SELECT order_id, order_date, total
FROM orders
WHERE (order_date, order_id) < ('2024-03-15', 1042)
ORDER BY order_date DESC, order_id DESC
LIMIT 10;This query continues after the order dated 2024-03-15 with ID 1042. It works with the same descending sort and requires suitable indexes for good performance.
Exercise
A page has 20 rows, and the application requests page 6. What OFFSET should the query use, and why should its ORDER BY include a tie-breaker?
Show answer
It should use OFFSET 100 because (6 - 1) * 20 = 100. The tie-breaker gives rows with equal primary sort values a stable order, making page boundaries predictable.