SQL error · Extra rows appear

Pagination repeats or skips rows

Problem: The ORDER BY allows ties, or the rows change between requests.

Solution: Add a unique key to the ordering; if there are concurrent changes, consider a cursor or a consistent snapshot.

Why it happens: pagination repeats or skips rows

Incomplete ordering
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.

Self-contained query · SQLite
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;
Query result
idprice
310
420

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.

What the problem looks like

No error appears: the same row shows up on two pages, or a row never shows up while paging.

Since the engine gives no warning, go through every page and check that each id appears exactly once.

How to fix it in SQL

Deterministic order
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.