Why it happens: pagination repeats or skips rows
SELECT id, name, price
FROM products
ORDER BY price
LIMIT 10 OFFSET 10;If several products have the same price, that column does not on its own decide which row comes first within the tie. Two separate queries may arrange those rows differently.
Check the result with small data
Three products share a price: we need a tiebreaker.
WITH products(id, price) AS (
VALUES (1,10), (2,10), (3,10), (4,20)
)
SELECT id, price FROM products
ORDER BY price, id LIMIT 2 OFFSET 2;| id | price |
|---|---|
| 3 | 10 |
| 4 | 20 |
Ordering by price, id determines exactly which rows go on the second page. If rows are inserted between queries, OFFSET can still shift the results.
Check your understanding: Which column decides the order among the rows with a price of 10?
Show answer
id: within price 10 it puts the rows in the order 1, 2, 3.
How to fix it in SQL
SELECT id, name, price
FROM products
ORDER BY price, id
LIMIT 10 OFFSET 10;If id is unique, the combination price, id creates a total order: rows are sorted by price first, and any ties are then broken by the id.
How to check it
Page through tied values and check that no id repeats. Also test inserts between requests.
How to avoid it next time
A tiebreaker fixes the order, but OFFSET can still shift when the data changes.
Related cases worth checking
PostgreSQL uses LIMIT/OFFSET. SQL Server uses ORDER BY ... OFFSET ... FETCH. The syntax differs, but both document the need for a predictable order to page reliably.
A unique order does not freeze the data. If rows are inserted, deleted or changed between the query for one page and the next, the set itself can change. SQL Server documents that stability between requests also depends on the underlying data not changing, or on using a suitable transactional strategy.