Interview Question

Write pagination query

Pagination needs a deterministic ORDER BY and a defined page method.

💡 Concept ✅ Quick Revision 🗃️ SQL

Answer

A pagination query returns a limited ordered slice of rows. • ORDER BY must provide a stable sequence. • OFFSET pagination skips earlier rows. • Keyset pagination is often more efficient and stable for deep or changing result sets.

💡 SQL Example

SELECT order_id, ordered_at FROM orders ORDER BY ordered_at DESC, order_id DESC OFFSET 20 ROWS FETCH FIRST 10 ROWS ONLY;

Result

The third page of ten ordered rows

⚡ Quick Revision

Pagination needs a deterministic ORDER BY and a defined page method.