Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
The delivery log is moving to infinite scroll, and OFFSET is not going to survive it: the further a user scrolls the slower each page gets, because the engine still produces every row it skips.
The client now sends back the last row it displayed — delivered_on = '2026-03-04', delivery_id = 25. Return the next five rows after that one, in the same newest-first order, without using OFFSET.
Result columns · in this order
delivery_id | Delivery identifier. |
city | City the delivery was made in. |
delivered_on | Date the delivery completed. |
minutes | Delivery time in minutes. |
How to approach it
Write the predicate in two branches: an earlier date, or the same date with a smaller id.
Sample input
| delivery_id | city | courier | minutes | delivered_on |
|---|---|---|---|---|
| 1 | Bengaluru | Asha | 18 | 2026-03-01 |
| 2 | Bengaluru | Asha | 22 | 2026-03-01 |
| 3 | Bengaluru | Rohit | 26 | 2026-03-02 |
| 4 | Bengaluru | Asha | 28 | 2026-03-02 |
| 5 | Bengaluru | Rohit | 30 | 2026-03-03 |
| 6 | Bengaluru | Meera | 30 | 2026-03-03 |
| 7 | Bengaluru | Meera | 34 | 2026-03-04 |
| 8 | Bengaluru | Rohit | 41 | 2026-03-04 |
| 9 | Bengaluru | Asha | 150 | 2026-03-05 |
| 10 | Pune | Kabir | 20 | 2026-03-01 |
| 11 | Pune | Kabir | 24 | 2026-03-01 |
| 12 | Pune | Nita | 26 | 2026-03-02 |
| 13 | Pune | Nita | 30 | 2026-03-02 |
| 14 | Pune | Kabir | 33 | 2026-03-03 |
| 15 | Pune | Nita | 36 | 2026-03-03 |
| 16 | Pune | Kabir | 45 | 2026-03-04 |
| 17 | Pune | Nita | 62 | 2026-03-05 |
| 18 | Pune | Kabir | 240 | 2026-03-05 |
| 19 | Chennai | Vikram | 19 | 2026-03-01 |
| 20 | Chennai | Priya | 23 | 2026-03-01 |
| 21 | Chennai | Vikram | 27 | 2026-03-02 |
| 22 | Chennai | Priya | 30 | 2026-03-03 |
| 23 | Chennai | Vikram | 38 | 2026-03-03 |
| 24 | Chennai | Priya | 52 | 2026-03-04 |
| 25 | Chennai | Vikram | 71 | 2026-03-04 |
| 26 | Chennai | Priya | 96 | 2026-03-05 |
26 rows — scroll inside the table to see them all.
Expected output
| delivery_id | city | delivered_on | minutes |
|---|---|---|---|
| 24 | Chennai | 2026-03-04 | 52 |
| 16 | Pune | 2026-03-04 | 45 |
| 8 | Bengaluru | 2026-03-04 | 41 |
| 7 | Bengaluru | 2026-03-04 | 34 |
| 23 | Chennai | 2026-03-03 | 38 |
5 rows — all rows shown.
Constraints
OFFSET. The whole point is that the query does not count through skipped rows.delivered_on descending, then delivery_id descending.Worked example
The cursor is (2026-03-04, 25). Everything on an earlier date qualifies immediately. Within 2026-03-04 itself, only the rows with a smaller id qualify — 24, 16, 8 and 7 — because the sort puts higher ids first.
That is the whole predicate: delivered_on < cursor_date OR (delivered_on = cursor_date AND delivery_id < cursor_id). The strict < on the second branch is what stops delivery 25 returning on its own next page.
The result is identical to LIMIT 5 OFFSET 5 — five rows starting at delivery 24 — but the engine can seek straight to the cursor on an index of (delivered_on, delivery_id) instead of walking the rows above it.
What this tests
Whether you can translate a sort order into a cursor predicate — the part people get wrong, usually by comparing on only one of the two sort keys.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.