In the SQL track's lesson Limiting Results, page 2 of an online shop was just LIMIT 10 OFFSET 10. Easy. The trouble only appears in a busy shop: page 10,000 gets slower, and an order that has just arrived can make one row appear twice.
A query's result can be split into small pages, say 5 orders per page, instead of sending 100,000 orders to the browser at once. This splitting is called pagination. There are two ways to do it, and the difference is like two ways of continuing to read a book. The first remembers a page number: "I got to page 37". The second uses a bookmark: "I stopped here". If someone puts a few new pages in at the front of the book, page 37 now holds something else, but your bookmark still sits in the right place. In the database, the page number is offset pagination, the bookmark is cursor pagination, and the pages put in at the front are new orders arriving.
The table we're using
Sunset Coffee's orders table holds 12 orders, id 1 to 12 (#12 means the order with id 12). The history page shows the newest first, so the sort order is ORDER BY id DESC: #12, #11, #10, and so on down to #1.
Offset pagination: LIMIT and OFFSET
With 5 orders per page, these queries fetch the three pages of that history:
-- page 1
SELECT * FROM orders ORDER BY id DESC LIMIT 5 OFFSET 0; -- #12, #11, #10, #9, #8
-- page 2
SELECT * FROM orders ORDER BY id DESC LIMIT 5 OFFSET 5; -- #7, #6, #5, #4, #3
-- page 3
SELECT * FROM orders ORDER BY id DESC LIMIT 5 OFFSET 10; -- #2, #1
LIMIT 5means take at most 5 rows.OFFSET 5means skip the first 5 rows, then start taking.ORDER BYis a must. WithoutORDER BY, the database may return rows in any order, and pages 1 and 2 can hold the same rows.
The OFFSET value comes from the page number: OFFSET = (page - 1) × perPage, where perPage is the number of rows per page. Page 1 skips 0 rows, page 2 at 10 per page skips 10 rows, page 5 at 20 per page skips 80 rows.
This is called offset-based pagination, the method that uses page numbers. In Laravel, Order::orderByDesc('id')->paginate(5) reads ?page=2 from the URL, then sends LIMIT 5 OFFSET 5. Laravel also sends one COUNT(*) query to calculate how many pages there are. simplePaginate(5) doesn't send that COUNT(*) query, so its page only has previous and next buttons.
Far pages get slower
OFFSET can't jump straight to the rows it needs. For OFFSET 99990, the database reads 99,990 rows, throws them all away, and only then takes the next 10. An index doesn't help, because the database still has to count the rows one by one. The query plan shows the same thing:
EXPLAIN QUERY PLAN
SELECT * FROM orders ORDER BY id DESC LIMIT 10 OFFSET 99990;
SCAN orders
SCAN orders means the database walks through the rows one by one, instead of jumping through an index (SEARCH).
In the Lab, there are 100,000 orders in real SQLite. Page 1 takes about 0.1 ms and page 10,000 about 1.5 ms: over ten times slower, though both hold just 10 rows. The exact numbers depend on your device. In a production database with wide rows (many columns) and queries that use JOIN, that gap can grow into seconds.
Shifting data: shown twice or skipped
You've seen page 1: #12 to #8. Then order #13 arrives, and you ask for page 2 with OFFSET 5. The 5 newest rows are now #13, #12, #11, #10, #9, so those five are the rows that get skipped. Page 2 now holds:
page 2: #8, #7, #6, #5, #4
#8 shows up again on page 2, though it was already on page 1. The opposite can happen too. If order #12 is deleted after you've seen page 1, the five newest rows become #11 to #7. OFFSET 5 skips all five, so #7 never appears at all.
Cursor pagination: remember the last position
Instead of counting rows to skip, keep the id of the last row already shown, then ask for the rows after it:
-- page 1
SELECT * FROM orders ORDER BY id DESC LIMIT 5; -- #12 ... #8
-- next page: after #8, the last row shown
SELECT * FROM orders WHERE id < 8 ORDER BY id DESC LIMIT 5; -- #7 ... #3
- That
8is the cursor: the last position already read, like the bookmark above. This is called cursor-based pagination, or keyset pagination. The two names mean the same idea. id < 8because the sort order is newest first (DESC). For oldest first (ORDER BY id), writeWHERE id > 8 ORDER BY id LIMIT 5.- If order #13 arrives while you are still reading page 1, the next page stays the same.
id < 8still gives #7 to #3, with nothing doubled and nothing skipped. - Fast on any page.
WHERE id < ?(the?is filled with the cursor value) uses the primary key index, so the database jumps straight to the position:SEARCH orders USING INTEGER PRIMARY KEY (rowid<?). Page 10,000 is as fast as page 1.
In Laravel: Order::orderByDesc('id')->cursorPaginate(5). The URL carries ?cursor=..., a string that holds that last id. The query is WHERE id < ? ORDER BY id DESC LIMIT 6. The sixth row is only used to check whether a next page exists.
The downside: cursor pagination can't jump to "page 37". There are only previous and next buttons. The column you sort by also has to be unique (no two rows with the same value), for example id.
When you sort by a column whose values can repeat, for example created_at, the cursor carries two values: the time and the id of the last row. A small example, in another orders table that holds only five orders: #2, #3 and #4 came in at the same minute, 10:00. The rows are sorted by time, then by id, newest first. Page 1 (LIMIT 2) holds #5 and #4, so the cursor is the time and id of #4:
-- cursor = ('2026-11-15 10:00', 4)
SELECT * FROM orders
WHERE (created_at, id) < ('2026-11-15 10:00', 4)
ORDER BY created_at DESC, id DESC
LIMIT 2; -- #3, #2
CREATE INDEX idx_orders_created_id ON orders (created_at, id);
(created_at, id) < (..., 4)means the time is earlier, or the time is the same and theidis smaller. The long form iscreated_at < ? OR (created_at = ? AND id < ?). SQLite, PostgreSQL and MySQL accept both forms.- With a one-value cursor,
WHERE created_at < '2026-11-15 10:00'jumps straight to #1, so #3 and #2 never appear. - The index
(created_at, id)follows theORDER BY, so the database can start right at the cursor's position.
Which one to pick
In the table below, infinite scroll is a page that loads new content by itself as you scroll down. A feed is a list that keeps growing, like a social media timeline.
Use offset (paginate) |
Use cursor (cursorPaginate) |
|---|---|
| You need page numbers, for example a table on an admin page | Infinite scroll and feeds |
| The data is small or rarely changes | Big tables, hundreds of thousands of rows or more |
| Users need to jump to a given page | Data keeps arriving while people are reading |
⚠️ Pages that repeat, skip, or miscount
- Forgetting
ORDER BY. The row order isn't guaranteed, so the same row can appear on two pages. - The wrong offset formula.
page * perPagemakes page 1 skip a whole page. The right one is(page - 1) * perPage. In the exercise,calculateOffset(1, 10)gives0,calculateOffset(5, 20)gives80. - A cursor on a column that isn't unique. If you sort by
created_atalone, other orders with the same time as the last row can get skipped. Addidas the second sort column:ORDER BY created_at DESC, id DESC. - The cursor pointing the wrong way. A descending (
DESC) sort usesid < cursor, an ascending sort usesid > cursor.
TL;DR: Pagination splits a query's result into small pages, and there are two ways to do it. Offset pagination uses LIMIT (take at most so many rows) and OFFSET (skip so many rows first), with OFFSET = (page - 1) × perPage (the number of rows per page). It uses page numbers (paginate() in Laravel). But it gets slower on far pages, because the database reads and throws away every row before the offset. Rows can also appear twice or be skipped when data is added or deleted while people move between pages. Cursor pagination (or keyset, cursorPaginate() in Laravel) keeps the id of the last row shown, then asks for the rows after it. If the last id is 8 and the rows are sorted newest first, the query is WHERE id < 8 ORDER BY id DESC LIMIT 5. It's fast on any page and stable (no row doubled or skipped), but it can't jump to a page number.
⚡ Offset: page numbers
Write:
-- page 2
SELECT * FROM orders ORDER BY id DESC LIMIT 5 OFFSET 5; -- #7, #6, #5, #4, #3
Meaning: Pagination splits a query's result into pages. Offset pagination is the method that uses page numbers. LIMIT 5 takes at most 5 rows, OFFSET 5 skips the first 5 rows, orders #12 to #8 (#12 = the order with id 12). The formula is OFFSET = (page - 1) × perPage (the number of rows per page), so page 1 skips 0 rows.
⚡ Shifting data
Guess first: you've seen page 1 (#12 to #8), then order #13 arrives. Where does page 2 with OFFSET 5 start?
At #8 again. The five newest rows are now #13 to #9, so OFFSET 5 skips all five, and #8 appears twice.
Meaning: OFFSET counts positions, not orders, so a new order shifts every position by one. Offset also gets slower on far pages, because the database reads and throws away every row before the offset.
⚡ Cursor: remember the last position
Write:
SELECT * FROM orders WHERE id < 8 ORDER BY id DESC LIMIT 5; -- #7 ... #3
Meaning: Cursor pagination keeps the id of the last row shown, here #8, as the cursor, then asks for the rows after it. It's id < 8 because the rows are sorted newest first. It's fast on any page and stable while data keeps arriving, but it can't jump to a page number.