Databases sql Intermediate New

Cursor-based Pagination Query

Implement cursor-based (keyset) pagination in SQL — faster than OFFSET for large tables.

sql
-- First page
SELECT id, title, published_at
FROM modules
WHERE is_published = true
ORDER BY published_at DESC, id DESC
LIMIT 20;

-- Next page (pass last row's values as cursor)
SELECT id, title, published_at
FROM modules
WHERE is_published = true
  AND (published_at, id) < ('2025-03-15 10:00:00', 42)
ORDER BY published_at DESC, id DESC
LIMIT 20;

Prerequisites

  • PostgreSQL >= 12 or MySQL >= 8

Pro Tips

  • → Encode cursor as base64(JSON({published_at, id})) in your API.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
sql
Level
Intermediate
Read time
4 min
Published
Aug 2026
License
Commercial License

Newsletter

New modules every week, directly to your inbox.