SQL Practice Logo

SQLPractice Online

LIMIT & OFFSET: Mistakes

Module: SQL Fundamentals

SELECT * FROM products LIMIT 10;

SELECT * FROM products ORDER BY product_id LIMIT 10;

LIMIT without ORDER BY returns arbitrary rows - results change between queries. Database returns first 10 rows it finds (physical order), which is non-deterministic. Always use ORDER BY for consistent, predictable results.

Always ORDER BY indexed column before LIMIT. Ensures consistent results and enables efficient pagination.

High

No error, but unpredictable results

SELECT * FROM products ORDER BY price LIMIT 10 OFFSET 10000;

SELECT * FROM products WHERE product_id > last_seen_id ORDER BY product_id LIMIT 10;

Large OFFSET is slow - database scans 10000 rows before returning 10. For deep pagination, use keyset pagination: WHERE id > last_id. This is constant time regardless of page depth.

Use keyset pagination for deep pages: WHERE id > cursor ORDER BY id LIMIT n. Avoid OFFSET > 1000.

High

No error, but 100x slower for deep pages