ORDER BY with LIMIT and OFFSET Pagination
Lesson 7 of 33 in the SQL / MySQL learning path.
How do I page through sorted SQL results?
A class roster that returns hundreds of students needs pages. ORDER BY id gives the list a stable sequence, LIMIT 20 keeps one page, and OFFSET says how many rows to skip before that page starts. Page numbering is arithmetic: the offset is the page size times one less than the page number. Page 1 is LIMIT 20 OFFSET 0. Page 2 is LIMIT 20 OFFSET 20. This script loads 25 students so the first page is full and the second page returns the five people who remain.
ORDER BY should use an indexed column. id is the primary key, so MySQL can walk the rows in order. OFFSET 20 is cheap, while a deep offset still reads and discards every skipped row before it returns the page. Keyset pagination, asking for ids greater than the last id already shown, stays fast on those deep pages.
Syntax & Parameters
SELECT column_list
FROM table_name
ORDER BY id
LIMIT 20 OFFSET 0;
SELECT column_list
FROM table_name
ORDER BY id
LIMIT 20 OFFSET 20;
Example
CREATE DATABASE IF NOT EXISTS school CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE school;
DROP TABLE IF EXISTS enrollments;
DROP TABLE IF EXISTS courses;
DROP TABLE IF EXISTS users;
CREATE TABLE users (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(120) NOT NULL,
email VARCHAR(254) NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY uq_users_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO users (name, email, password_hash, created_at) VALUES
('Student 01', 's01@school.test', 'sample-hash-not-a-secret', '2026-01-01 08:00:00'),
('Student 02', 's02@school.test', 'sample-hash-not-a-secret', '2026-01-02 08:00:00'),
('Student 03', 's03@school.test', 'sample-hash-not-a-secret', '2026-01-03 08:00:00'),
('Student 04', 's04@school.test', 'sample-hash-not-a-secret', '2026-01-04 08:00:00'),
('Student 05', 's05@school.test', 'sample-hash-not-a-secret', '2026-01-05 08:00:00'),
('Student 06', 's06@school.test', 'sample-hash-not-a-secret', '2026-01-06 08:00:00'),
('Student 07', 's07@school.test', 'sample-hash-not-a-secret', '2026-01-07 08:00:00'),
('Student 08', 's08@school.test', 'sample-hash-not-a-secret', '2026-01-08 08:00:00'),
('Student 09', 's09@school.test', 'sample-hash-not-a-secret', '2026-01-09 08:00:00'),
('Student 10', 's10@school.test', 'sample-hash-not-a-secret', '2026-01-10 08:00:00'),
('Student 11', 's11@school.test', 'sample-hash-not-a-secret', '2026-01-11 08:00:00'),
('Student 12', 's12@school.test', 'sample-hash-not-a-secret', '2026-01-12 08:00:00'),
('Student 13', 's13@school.test', 'sample-hash-not-a-secret', '2026-01-13 08:00:00'),
('Student 14', 's14@school.test', 'sample-hash-not-a-secret', '2026-01-14 08:00:00'),
('Student 15', 's15@school.test', 'sample-hash-not-a-secret', '2026-01-15 08:00:00'),
('Student 16', 's16@school.test', 'sample-hash-not-a-secret', '2026-01-16 08:00:00'),
('Student 17', 's17@school.test', 'sample-hash-not-a-secret', '2026-01-17 08:00:00'),
('Student 18', 's18@school.test', 'sample-hash-not-a-secret', '2026-01-18 08:00:00'),
('Student 19', 's19@school.test', 'sample-hash-not-a-secret', '2026-01-19 08:00:00'),
('Student 20', 's20@school.test', 'sample-hash-not-a-secret', '2026-01-20 08:00:00'),
('Student 21', 's21@school.test', 'sample-hash-not-a-secret', '2026-01-21 08:00:00'),
('Student 22', 's22@school.test', 'sample-hash-not-a-secret', '2026-01-22 08:00:00'),
('Student 23', 's23@school.test', 'sample-hash-not-a-secret', '2026-01-23 08:00:00'),
('Student 24', 's24@school.test', 'sample-hash-not-a-secret', '2026-01-24 08:00:00'),
('Student 25', 's25@school.test', 'sample-hash-not-a-secret', '2026-01-25 08:00:00');
SELECT id, name, email
FROM users
ORDER BY id
LIMIT 20 OFFSET 0;
SELECT id, name, email
FROM users
ORDER BY id
LIMIT 20 OFFSET 20;
Expected Output
Page 1, LIMIT 20 OFFSET 0: 20 rows, id 1 Student 01 through id 20 Student 20 id | name | email 1 | Student 01 | s01@school.test 20 | Student 20 | s20@school.test 20 rows in set Page 2, LIMIT 20 OFFSET 20: 5 rows id | name | email 21 | Student 21 | s21@school.test 22 | Student 22 | s22@school.test 23 | Student 23 | s23@school.test 24 | Student 24 | s24@school.test 25 | Student 25 | s25@school.test 5 rows in set
Line-by-Line Walkthrough
- The script creates users and inserts 25 students with ids 1 through 25 so both pages return rows.
- ORDER BY id follows the primary key, which is an index, so the sort matches the order the pages will keep using.
- Page size is 20. The offset formula is (page number - 1) times 20.
- Page 1 uses LIMIT 20 OFFSET 0, which skips nobody and returns ids 1 through 20.
- Page 2 uses LIMIT 20 OFFSET 20, which skips the first 20 ids and returns ids 21 through 25.
- The second page has five rows because only 25 students exist. LIMIT is a maximum, not a promise that the page is full.
- The column list is id, name, and email. password_hash is not part of a roster page.
- Without ORDER BY, MySQL may return a different set of 20 rows on each run. Pagination needs a stable order.
- OFFSET 20 is small. A deep page such as LIMIT 20 OFFSET 9980 still reads and throws away 9980 rows, which is why large offsets get slow.
- A keyset page asks for WHERE id > 20 ORDER BY id LIMIT 20 and uses the last id the user already saw. That seek uses the primary key and stays cheap as the roster grows.
Common Pitfalls & Security
OFFSET pagination looks correct on page 2 of a 25-row class and becomes expensive on page 500 of a full student body. MySQL still walks every skipped row. The order also has to be unique and stable, or a student can appear on two pages when a new row is inserted near the top while someone is clicking Next. Keep the page size fixed and the ORDER BY tied to an index.
- ORDER BY with no index, or ORDER BY a column that is not indexed, forces a sort of the matching rows. Order by the primary key id, or add an index that matches the sort.
- A large OFFSET does not jump. LIMIT 20 OFFSET 9980 reads 10000 rows and returns the last 20. Seek with WHERE id > last_seen_id when the pages get deep.
- Inserts and deletes while someone pages can duplicate or skip a student under OFFSET. Keyset paging on id stays aligned with what the user already saw.
- LIMIT without ORDER BY returns an arbitrary 20 rows. Do not page a roster that way.
- An UPDATE or DELETE that omits WHERE is a catastrophe and will rewrite or empty users. Paging queries in this lesson only read, and any edit still needs a primary-key WHERE.
- A huge page size, such as LIMIT 100000, is a full table dump wearing a paging costume. Keep the page near the size of the screen.
Best Practices & Modern Standards
Order by an indexed column, use a stable tie-breaker such as id, and compute the offset as (page - 1) times the page size. Show page 1 as LIMIT 20 OFFSET 0 and page 2 as LIMIT 20 OFFSET 20, and expect the last page to be short. For deep pages, switch to a keyset: WHERE id > :last_id ORDER BY id LIMIT 20. The index lesson shows how to confirm that plan with EXPLAIN.
Laravel can render this style of page links through laravel-pagination. The SQL underneath is still an ordered LIMIT. Pass the page size from the application, and cap it so a client cannot request every student in one response.
Real-World Use
The student directory in a school enrollment system is a paged read of users. Courses and enrollments have their own lists, and each list needs the same paging math. A staff page of 20 students starts at offset 0 and the next page starts at offset 20. When the directory grows to tens of thousands of students, the screen should seek forward from the last id instead of skipping a huge offset, while the course catalog and the enrollment roster keep their own ordered pages.
Frequently Asked Questions
How is the offset for page 2 calculated?
Offset equals (page number - 1) times the page size. With a page size of 20, page 1 uses OFFSET 0 and page 2 uses OFFSET 20. Page 3 would use OFFSET 40.
Why does page 2 return five rows?
The table has 25 students. The first page takes ids 1 through 20. LIMIT 20 OFFSET 20 asks for up to 20 more rows and finds ids 21 through 25. LIMIT is a ceiling.
Why is a large OFFSET slow?
MySQL reads the skipped rows and discards them. OFFSET 9980 with LIMIT 20 still walks those 9980 rows. A predicate on the last seen id, ordered by that indexed id, reads only the next page.
Which column should ORDER BY use?
Use an indexed column. This lesson orders by id, the primary key. If you sort by created_at, add an index on created_at and include id as a tie-breaker so two students who registered together do not trade places.