LIMIT & OFFSET
LIMIT caps the number of returned rows.
Introduction
LIMIT caps the number of returned rows. OFFSET skips rows. Together they are used for pagination — but for large datasets, OFFSET pagination becomes slow and you should switch to keyset pagination.
Learning objective: Apply LIMIT & OFFSET correctly in a TechLearningPRO production database — with clear query intent, safe defaults, and failure modes you can explain in review.
Treat every example as production-shaped SQL: explicit column lists, deterministic predicates, and indexes that match the access path. Prefer PostgreSQL dialect notes when dialects diverge; call out MySQL differences when they matter.
Purpose of this lesson
Apply LIMIT & OFFSET correctly in a TechLearningPRO production database — with clear query intent, safe defaults, and failure modes you can explain in review.
Understanding the topic
Core concepts to understand:
SELECT ... LIMIT 20 OFFSET 0;— page 1.... LIMIT 20 OFFSET 40;— page 3.- OFFSET is O(offset) — page 1000 reads 20,000 rows.
- Keyset pagination:
WHERE id < last_seen_id ORDER BY id DESC LIMIT 20— O(1). - MySQL & Postgres support LIMIT; SQL Server uses
OFFSET ... FETCH NEXT.
Step-by-step explanation
- State the business question LIMIT & OFFSET answers and the tables involved.
- Read the workflow diagram and map each stage to parser, optimizer, or storage.
- Write the smallest correct statement (or design change) for a TechLearningPRO Orders/learners dataset.
- Predict the failure mode: wrong cardinality, missing index, or unsafe write — then verify.
Syntax reference
Visual workflow / architecture:
OFFSET pagination (slow at depth)page 1 page 2 page 3 ... page 1000[1-20] [21-40] [41-60] ... [skip 19,980 rows!]Keyset pagination (fast at any depth)WHERE id < last_id ORDER BY id DESC LIMIT 20─── only reads 20 rows, always.
SELECT <cols> FROM <table> ORDER BY <col> LIMIT <count> OFFSET <skip>;
LIMITMaximum number of rows to return.OFFSETSkip N rows before returning — slow at depth.KeysetPrefer WHERE id < last_id ORDER BY id DESC LIMIT n.
SELECT id, titleFROM postsORDER BY id DESCLIMIT 2 OFFSET 2; -- page 2 (size 2)
| id | title |
|---|---|
| 5 | Indexes 101 |
| 4 | ACID explained |
| 3 | JOINs deep dive |
| 2 | WHERE tips |
| 1 | Hello SQL |
| id | title |
|---|---|
| 3 | JOINs deep dive |
| 2 | WHERE tips |
LIMIT + OFFSET = a sliding window over the ordered result.
Informative example
Worked SQL example:
SELECT id, titleFROM postsORDER BY id DESCLIMIT 2 OFFSET 2; -- page 2 (size 2)
Real-world use
Twitter, Instagram and Stripe Dashboard all use keyset pagination because OFFSET breaks beyond the first 100 pages. Use cursors or last-id pointers in your APIs.
Security implications
SQL is a trust boundary. Application code must never concatenate untrusted input into statements.
- Use bind parameters / prepared statements — never string-built SQL from request bodies.
- Least-privilege DB roles: app users should not own DDL in production.
- Avoid returning raw DB errors to clients; log server-side with a correlation id.
Best practices
- Use OFFSET for shallow pagination only (first 100 pages).
- Switch to keyset pagination for infinite scroll / deep pages.
- Always have a stable ORDER BY when paginating.
Common mistakes
- OFFSET on a 10M row table page 5000 — multi-second query.
- Pagination without ORDER BY → unstable, duplicate rows.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionQ1. What is keyset pagination?+
Answer
2IntermediateQuestionQ2. Why is OFFSET slow at depth?+
Answer
3IntermediateQuestionQ3. SQL Server equivalent of LIMIT.+
Answer
4AdvancedQuestionQ4. Why must paginated queries have ORDER BY?+
Answer
5AdvancedQuestionQ5. Use case for OFFSET pagination.+
Answer
Hands-on exercise
Hands-on + interview drill:
- Implement the smallest correct example for LIMIT & OFFSET on an Orders-like schema.
- Q1. What is keyset pagination?
- Q2. Why is OFFSET slow at depth?
- Q3. SQL Server equivalent of LIMIT.
- Q4. Why must paginated queries have ORDER BY?
- Q5. Use case for OFFSET pagination.
Summary
LIMIT & OFFSET is a production SQL skill: state the question, write the precise statement, and prove it with an execution plan or constraint — not with lucky result sets.