intermediateSELECT Queries — WHERE, GROUP BY, HAVING, ORDER BY

Why should you avoid OFFSET for pagination in large tables?

OFFSET scans and discards n rows before returning results. With OFFSET 100000, the DB reads and throws away 100,000 rows on every page load — O(n) performance. Keyset pagination (cursor-based) is O(log n): SELECT * FROM orders WHERE order_id > last_seen_id ORDER BY order_id LIMIT 20. The application passes the last seen ID. The query uses the primary key index to jump directly. Limitation of keyse

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to A report query that aggregates 50 million orders is taking 30 seconds. How do you optimize it?← Back to all SQL questions