Recipe · SQL recipes

Keyset pagination in SQL: carrying on from the last row

To fetch the next page, filter for rows after the last row's key and keep the same ORDER BY. The cursor must include the unique tiebreaker.

Before continuing: This entry is part of the pagination guide. If you are new to the topic, start with LIMIT.

Query

The first query needs no cursor: it just sets a total order and limits the block.

First page
SELECT id, date, total
FROM orders
ORDER BY date DESC, id DESC
LIMIT 5;

The cursor for continuing is date = '2025-06-08' e id = 114.

Next page
SELECT id, date, total
FROM orders
WHERE date < '2025-06-08'
   OR (date = '2025-06-08' AND id < 114)
ORDER BY date DESC, id DESC
LIMIT 5;

How it works

With OFFSET an application typically asks «skip 180 rows and give me the next 20». With keyset it asks «carry on after the last order I already showed». That difference stops a row's absolute position from being the starting point of every query.

The pattern fits feeds, histories, chronological lists and «load more» buttons especially well. It is less convenient if you need to jump straight to an arbitrary numbered page.

Cases that change the result

Why you need a unique tiebreaker

If you sort only by date and two orders share the same date, the cursor cannot tell which rows of that tie have already appeared. Adding id (if it is unique) creates a total order and lets you carry on within the same date.

Changing the ORDER BY between pages

The cursor filter and the ORDER BY describe the same walk through the data. If you change columns, direction or collation without regenerating the cursor, you can repeat or skip rows.

What it improves over OFFSET, and what it does not

PostgreSQL documents that rows discarded by a OFFSET still have to be computed inside the server. Keyset avoids depending on an ever-growing number of rows to skip, and lets an index that matches the filter and the ordering be a good candidate for continuing the walk.

It also reduces the positional shift caused by inserts before the cursor. However, it does not, on its own, create an immutable snapshot of the data: later changes can still alter which rows exist or what values they hold. If you need snapshot consistency across many requests, that belongs to the transactional and application design.

Candidate index and check

Index hypothesis
CREATE INDEX idx_orders_date_id
ON orders (date DESC, id DESC);

is a reasonable hypothesis for this pattern, not a promise of performance. Check the plan with your engine's tool and measure with representative data. How index directions are handled, and the exact plan, vary between engines.

Key differences between engines

Keyset is not a special SQL clause: it combines comparisons, WHERE, ORDER BY and a way of limiting rows. PostgreSQL, MySQL and SQLite allow LIMIT; SQL Server can limit with TOP or with OFFSET/FETCH depending on the design. The way parameters are written (:name, ?, @name) belongs to the driver or framework.

Some engines support comparing row values, for example (date, id) < (?, ?). The explicit form with OR in this guide shows the logic without relying on that shortcut and makes it easier to adapt to different dialects.