Knowledge base / SQL
SQL OFFSET: skipping rows and pagination pitfalls
OFFSET skips rows in an ordered result. I separate the dialect syntax and explain why unique ordering alone cannot stabilise pages over changing data.

When I need a second page, I must define its size and ordering. OFFSET specifies how many initial result rows to skip. It does not search for an identifier or delete rows from a table. The examples here read a small synthetic dataset; they do not establish performance guarantees for an application.
Choose the syntax for your engine
For PostgreSQL 18, MySQL 8.4 and SQLite, I write a second page of two rows as follows:
SELECT id, name
FROM users
ORDER BY id
LIMIT 2 OFFSET 2;
This clause order is documented by PostgreSQL, MySQL and SQLite. PostgreSQL also accepts the reversed order of those two clauses, but I would not present that as shared syntax for the other engines. I use LIMIT ... OFFSET ... consistently here.
SQL Server 2012 and later use a different form for this example:
SELECT id, name
FROM users
ORDER BY id
OFFSET 2 ROWS FETCH NEXT 2 ROWS ONLY;
Microsoft documents OFFSET and FETCH as part of ORDER BY. I would not append LIMIT to that query. Similar-looking fragments across databases do not establish that an entire query can be transferred unchanged.
Two skipped rows are not an identifier
Assume that users contains the following data. The id column is unique and non-null, and the dataset does not change during the read.
| id | name |
|---|---|
| 11 | Ada |
| 14 | Ben |
| 18 | Celina |
| 23 | Dawid |
| 31 | Ewa |
After sorting by id, we skip Ada and Ben, the first two rows. The following two rows are Celina and Dawid. Both queries above therefore produce this result for the example:
| id | name |
|---|---|
| 18 | Celina |
| 23 | Dawid |
Ewa is outside this page because its limit is two. Gaps between identifiers do not affect the interpretation. OFFSET 2 does not mean id > 2 or start at id = 2. It refers to a position in the result after the query’s other logic has been applied, rather than the value of a key.
Page numbers and boundary cases
With page numbering starting at one, the skip count is (page_number - 1) * page_size. A size of two gives offsets zero, two and four. The first page contains Ada and Ben, the second Celina and Dawid, and the third only Ewa. The limit is a maximum, rather than a promise that every page will be full.
Skipping beyond the available result produces an empty page. In an application, I would constrain page numbers and sizes to agreed positive values and pass parameters through mechanisms supported by its driver. Parameter syntax depends on the engine and library. The fixed numbers here make the examples easier to read; they are not an application-level input-handling design.
MySQL and SQLite also support a comma form whose argument order can confuse readers: LIMIT 2, 5 skips two rows and retrieves at most five. I prefer the explicit OFFSET form. I would also avoid relying on implicit handling of negative values, since engines differ in how they interpret or reject them.

Ordering must resolve ties
If the query orders only by surname, two people sharing a surname can exchange positions at a page boundary. I would add a unique tie-breaker, for example ORDER BY surname, id. Our small dataset needs only ORDER BY id, because uniqueness is an explicit assumption of this example.
That does not freeze the table. Suppose you read the first page, IDs 11 and 14. Before the second request, someone deletes ID 11. An offset of two now skips IDs 14 and 18, returning 23 and 31. You missed ID 18 while browsing, despite having a uniquely determined ordering for each individual request.
Microsoft separately describes stable pagination requirements: unique ordering and unchanged underlying data, or an appropriately consistent view across reads. Transaction design depends on the application. I would not keep a transaction open through an entire user session without deliberately designing that mechanism and considering what the application actually needs from consistency.
When I would consider key-based pagination
When moving forward through an ascending, unique id, I can retain the last displayed key. After the first page, it is 14. In PostgreSQL, MySQL and SQLite, the next read can be written as:
SELECT id, name
FROM users
WHERE id > 14
ORDER BY id
LIMIT 2;
The example returns 18 and 23, including after ID 11 is removed. This ties the next page to a key value. It does not automatically support jumping to an arbitrary page number or provide a snapshot of the entire dataset. Changes to filter membership or ordering values still require thought.
For sorting across several columns, the continuation condition must reflect the full order, including ties and direction. I would not copy id > last_id into a list ordered by a date. The continuation key and the displayed order would then be describing different sequences, so our simple example would no longer justify the behaviour.
PostgreSQL warns that skipped rows still require server work, so a large offset can be inefficient. I would not infer a universal page-count threshold or promise that adding an index guarantees a speedup. I would assess the target query’s plan and behaviour before selecting an approach for a particular system.
Exercise
Write down the result for LIMIT 2 OFFSET 4, then for LIMIT 2 OFFSET 5. The first contains Ewa; the second is empty. Next, explain why deleting the first row can change the second page. For the maximum returned row count itself, continue with LIMIT.
Key takeaways
Does OFFSET 2 start at the record with ID 2?
No. It skips two rows of the ordered result regardless of their identifiers. With LIMIT 2, it then returns at most two subsequent rows.
Does SQL Server use LIMIT?
For this example, SQL Server uses ORDER BY followed by OFFSET 2 ROWS FETCH NEXT 2 ROWS ONLY. LIMIT 2 OFFSET 2 is shown separately for PostgreSQL, MySQL and SQLite.
Does ORDER BY guarantee stable pages?
Ordering must resolve ties, but changes between independent requests can still shift row positions. Stable browsing also needs a deliberate approach to changes in the underlying data.
AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.
