Concept · Basic queries

OFFSET

OFFSET skips a number of rows in the result. For stable pagination it needs an ORDER BY with a unique tiebreaker.

Before continuing: This entry is part of the pagination guide. If you are new to the topic, start with LIMIT.

OFFSET example

Second page of products
SELECT id, name, price
FROM products
ORDER BY price, id
LIMIT 3 OFFSET 3;

The engine sorts by price and uses id to break ties. It then skips three rows and returns the next three.

What to look for: With six products in a stable order, LIMIT 3 OFFSET 3 returns positions 4, 5 and 6. If the data changes between pages, some rows may repeat or be skipped; for lists that change often, consider keyset pagination.

Check the result with small data

We sort four products and want the second page of two.

Self-contained query · SQLite
WITH products(id, name) AS (
  VALUES (1,'A'),(2,'B'),(3,'C'),(4,'D')
)
SELECT id, name FROM products
ORDER BY id LIMIT 2 OFFSET 2;
Query result
idname
3C
4D

OFFSET 2 skips the first two rows and LIMIT 2 returns the next two. Paginating over changing data can repeat or skip rows.

Check your understanding: What would LIMIT 2 OFFSET 0 return?

Show answer

The first two rows, 1 A and 2 B: OFFSET 0 skips none.

Common mix-ups with OFFSET

Paginating without a unique order

ORDER BY price does not decide the order between products with the same price. Add a stable key, such as ORDER BY price, id.

Large offsets can also be expensive, because the engine has to find and discard many rows. For long walks through the data, cursor pagination (for example, carrying on after the last id you saw) can scale better.

Key differences between engines

PostgreSQL, MySQL and SQLite accept the form LIMIT … OFFSET …. SQL Server builds OFFSET and FETCH within ORDER BYinto ORDER BY. The exact syntax differs, but the advice to fix a predictable order still applies.

A stable page needs a unique order

OFFSET says how many rows to skip before returning the next ones, and usually goes with LIMIT. Without ORDER BY, the engine promises no order at all, so “the second page” can contain different rows on two runs. Always define the order before paginating.

The order must also be unique. If you sort by a column with repeated values, tied rows can swap places between two queries, and a row can be repeated or lost across pages. Add a tiebreaker column, such as the id, at the end of the ORDER BY.

With OFFSET the engine still has to walk through the skipped rows, so very deep pages get slow, and data that changes between requests shifts the results. For long, active lists, keyset pagination, which filters on the last value seen, avoids both problems. Check a page in the middle that contains ties to confirm nothing overlaps.

Self-contained case to check in SQLite

There are ties on category; the id breaks them and makes the order deterministic.

Data and query · SQLite
WITH items(id, category) AS (
  VALUES (1, 'a'), (2, 'a'), (3, 'b'), (4, 'b'), (5, 'c')
)
SELECT id, category
FROM items
ORDER BY category, id
LIMIT 2 OFFSET 2;
Verified result of the self-contained case
idcategory
3b
4b

With the full sort order, the third and fourth rows are always 3 and 4. Sorting by category alone would let 3 and 4 swap places between runs.

Check your interpretation: Which rows would LIMIT 2 OFFSET 4 return with this order?

Show answer

Only one row, 5 (c): after skipping four rows there are no more left.

Technical questions: PostgreSQL: LIMIT and OFFSET (opens in a new tab).