Library › Coding AI › Software developers
Convert OFFSET paging to keyset pagination
Practice: Setting limits and rules. About 5 minutes.
About this exercise
The activity feed's OFFSET paging gets slow and skips rows when new items arrive. Rewrite it as keyset pagination: the client sends the last row's created_at and id as $1 and $2, workspace_id moves to $3, and the page size is now 25. Use a row comparison so ties on created_at don't skip rows, and keep the same columns and the deleted filter. Exactly 1 code block of at most 8 lines, under 25 words of text.
Common mistakes when you ask AI for this
- Comparing only the timestamp. created_at < $1 alone skips rows that share a timestamp, like the bulk imports. Try: Add: "Use the row comparison (created_at, id) < ($1, $2)."
- Old page size and order. The AI keeps LIMIT 50 and may drop id from the ORDER BY, so ties come back in random order. Try: Add: "ORDER BY created_at DESC, id DESC LIMIT 25."
- Renumbered or lost filters. The AI may keep workspace_id as $1 or drop the deleted filter. Try: Add: "workspace_id = $3; keep deleted_at IS NULL and the same columns."
- An essay on pagination. The AI explains keyset vs offset with several variants. Try: Add: "One query, one code block, no alternatives."
What a strong prompt includes
- Asks for keyset pagination
- Handles ties
- Gives the placeholders
- Gives the page size
- Keeps the filters
Related exercises
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input checks to your number guessing game (Coding AI · High school students)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)
- Add labels to a signup form without rewriting the page (Coding AI · High school students)