Search RU
Tasks / SQL / Keyset pagination of an event feed
Statement medium

Keyset pagination of an event feed

`events` is an activity feed. The cursor points at the last event already
shown: `created_at = DATE '2026-05-01'`, `id = 100`.

Return the next 3 feed events — the newest ones strictly older than the
cursor: `created_at` descending, then `id` descending for the same date.

Result columns: `id`, `created_at`, `payload`.

The cursor event itself and everything newer must not appear.

SQL · PostgreSQL
sqlkeyset-paginationtuple-comparisonorder-by
Schema
CREATE TABLE events (
    id integer PRIMARY KEY,
    created_at date NOT NULL,
    payload text NOT NULL
);
Data
INSERT INTO events (id, created_at, payload) VALUES
    (110, '2026-05-03', 'deploy'),
    (104, '2026-05-01', 'late-comment'),
    (100, '2026-05-01', 'cursor'),
    (99,  '2026-05-01', 'checkout'),
    (98,  '2026-05-01', 'signup'),
    (97,  '2026-04-30', 'search'),
    (96,  '2026-04-30', 'refund'),
    (95,  '2026-04-29', 'ping');
query.sql PostgreSQL 17
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

The cursor is a pair of values. Compare both columns together, not one at a
time.

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts