intermediateTop 30 Scenario-Based Questions

Your pagination query (OFFSET 1000000 LIMIT 20) is extremely slow. How do you fix it?

OFFSET N scans and discards N rows — O(n). For OFFSET 1,000,000: 1 million rows read and thrown away. Solution: Keyset pagination. Instead of OFFSET: WHERE id > last_seen_id ORDER BY id LIMIT 20. The query uses the primary key index — O(log n). In API: return last_id in response; next request uses it. For complex ORDER BY: use a tuple comparison: WHERE (salary, id) < (last_salary, last_id) ORDER B

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 NOT IN subquery is returning 0 rows even though you expect results. Why?← Back to all SQL questions