SQL Tutorial 0/85 lessons ~6 min read Lesson 14

    LIMIT & OFFSET

    LIMIT caps the number of returned rows.

    Course progress0%
    Focus
    13 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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

    1. State the business question LIMIT & OFFSET answers and the tables involved.
    2. Read the workflow diagram and map each stage to parser, optimizer, or storage.
    3. Write the smallest correct statement (or design change) for a TechLearningPRO Orders/learners dataset.
    4. Predict the failure mode: wrong cardinality, missing index, or unsafe write — then verify.

    Syntax reference

    Visual workflow / architecture:

    bash
    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.
    Syntax Pattern
    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.
    Live Example
    sql
    SELECT id, title
    FROM posts
    ORDER BY id DESC
    LIMIT 2 OFFSET 2; -- page 2 (size 2)
    posts
    idtitle
    5Indexes 101
    4ACID explained
    3JOINs deep dive
    2WHERE tips
    1Hello SQL
    page 2
    idtitle
    3JOINs deep dive
    2WHERE tips
    LIMIT / OFFSET
    page window over the result set

    LIMIT + OFFSET = a sliding window over the ordered result.

    Informative example

    Worked SQL example:

    sql
    SELECT id, title
    FROM posts
    ORDER BY id DESC
    LIMIT 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.

    5 questions
    1BeginnerQuestionQ1. What is keyset pagination?+

    Answer

    LIMIT caps rows; OFFSET skips them.
    2IntermediateQuestionQ2. Why is OFFSET slow at depth?+

    Answer

    OFFSET is O(offset) — slow at depth.
    3IntermediateQuestionQ3. SQL Server equivalent of LIMIT.+

    Answer

    Keyset pagination is O(1) — production grade.
    4AdvancedQuestionQ4. Why must paginated queries have ORDER BY?+

    Answer

    Keyset pagination is O(1) — production grade.
    5AdvancedQuestionQ5. Use case for OFFSET pagination.+

    Answer

    Keyset pagination is O(1) — production grade.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.