Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 52

LIMIT and OFFSET in MySQL

LIMIT and OFFSET

LIMIT caps returned rows; OFFSET skips rows from an ordered result. They are useful for previews, top-N reports and pagination, but correctness begins with deterministic ORDER BY.

Common learning data: students contains Aarav (X-A, 86.50, Active), Meera (X-A, 91.00, Active), Kabir (X-B, 74.00, Inactive) and Sana (X-B, 88.50, Active).
SELECT student_id, full_name, marks
FROM students
ORDER BY marks DESC, student_id ASC
LIMIT 2;
Meera | 91.00 Sana | 88.50

MySQL Syntax Forms

-- Return at most row_count rows
LIMIT row_count

-- Skip offset rows, then return row_count
LIMIT row_count OFFSET offset

-- MySQL comma form: offset first, count second
LIMIT offset, row_count
Avoid confusion: LIMIT 10 OFFSET 20 means count 10 after skipping 20; LIMIT 20, 10 means the same. Prefer the keyword form in teaching and maintainable code.

Page Calculation

For page size 2: page 1 offset 0, page 2 offset 2, page 3 offset 4. Formula:

offset = (page_number - 1) * page_size
SELECT student_id, full_name, marks
FROM students
ORDER BY marks DESC, student_id ASC
LIMIT 2 OFFSET 2;
Aarav | 86.50 Kabir | 74.00

Validate page as a positive integer, cap page_size and bind permitted numeric values through the driver where supported.

Deterministic Ordering

Without ORDER BY, storage or execution changes can move rows between pages. Even ORDER BY marks DESC is incomplete if marks tie. Add a unique final key:

ORDER BY marks DESC, student_id ASC

When data changes between page requests, offset pages can still show duplicates or miss rows. Choose pagination consistency according to the application.

Large OFFSET and Keyset Pagination

Deep offset pagination can require scanning and discarding many earlier rows. For a “next” feed ordered by student_id, keyset pagination is simpler:

SELECT student_id, full_name
FROM students
WHERE student_id > ?
ORDER BY student_id ASC
LIMIT 20;

Bind the last student_id from the previous page. For multi-column ordering, the seek condition must reproduce the complete ordering tuple. Keyset pagination is fast and stable for next/previous navigation but does not naturally jump to an arbitrary page number.

Mistakes and Practice

  • LIMIT without ORDER BY.
  • Reversing comma-form arguments.
  • No maximum page size.
  • Ignoring ties.
  • Using huge OFFSET for an endless feed.
  1. Return the top three marks deterministically.
  2. Write page 2 with size 2.
  3. Rewrite ID-based next-page navigation as keyset pagination.
  4. Explain why new inserts can shift offset pages.

Quick Summary

  • LIMIT caps rows and OFFSET skips ordered rows.
  • Use ORDER BY plus a unique tie-breaker.
  • Validate and cap pagination inputs.
  • Keyset pagination avoids large-offset discard cost for sequential navigation.

Official References

References reviewed 14 August 2026.

Frequently Asked Questions

What does LIMIT do in MySQL?
LIMIT caps the number of rows returned after filtering and ordering. It does not define which rows are first without ORDER BY.
What is OFFSET?
OFFSET tells MySQL how many ordered result rows to skip before returning the limited page.
How is page offset calculated?
For one-based page number p and page size n, offset = (p - 1) × n. Validate both inputs and set a maximum page size.
Why is a large OFFSET slow?
MySQL may still need to locate and discard many earlier rows. Deep pages can become expensive even though only a few rows are returned.
What is keyset pagination?
It requests rows after the last seen ordered key, such as WHERE student_id > ?, instead of counting and discarding all previous rows.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.