PostgreSQL LIMIT & OFFSET
LIMIT returns only the first n rows of a result, and OFFSET skips a number of rows before starting. Together they are used for "top N" lists and for splitting results into pages.
LIMIT
The three newest books:
SELECT title, published
FROM books
ORDER BY published DESC
LIMIT 3; title | published
----------------------+------------
The Map Thief | 2025-05-20
Winter in Sofia | 2024-12-01
Postgres in Practice | 2024-04-03
(3 rows)
Always combine LIMIT with ORDER BY. Without a defined order, "the first three rows" can be any three rows.
OFFSET
OFFSET n skips n rows. Books 4 to 6 by price:
SELECT title, price
FROM books
ORDER BY price DESC
LIMIT 3 OFFSET 3; title | price
------------------------+-------
Algorithms on a Napkin | 35.00
Empires of Sand | 27.00
The Age of Canals | 24.95
(3 rows)
Paging through results
A web page that shows 5 customers per page computes OFFSET (page - 1) * 5:
-- page 1
SELECT id, first_name, last_name FROM customers ORDER BY last_name, id LIMIT 5 OFFSET 0;
-- page 3
SELECT id, first_name, last_name FROM customers ORDER BY last_name, id LIMIT 5 OFFSET 10; id | first_name | last_name
----+------------+-----------
14 | Noah | Brown
2 | Ben | Carter
3 | Chloé | Dubois
13 | Mia | Fischer
6 | Farid | Haddad
(5 rows)
id | first_name | last_name
----+------------+-----------
12 | Liam | O'Brien
7 | Gina | Rossi
11 | Keiko | Sato
8 | Hugo | Silva
10 | Jamal | Wright
(5 rows)
Notice the id after last_name: if two customers had the same last name, the extra key keeps the order stable, so no one shows up twice or disappears between pages.
Tip: Large offsets are slow because the server still has to produce and throw away all the skipped rows. For big tables, "keyset pagination" is faster: remember the last value shown and ask for
WHERE (last_name, id) > ('Kim', 4) ORDER BY last_name, id LIMIT 5.
The standard form: FETCH FIRST
The SQL standard spells the same idea differently, and PostgreSQL accepts it too:
SELECT title, stock
FROM books
ORDER BY stock DESC
OFFSET 1 ROWS
FETCH FIRST 3 ROWS ONLY; title | stock
--------------------+-------
Moon Socks | 25
Murder at Low Tide | 20
Clockwork Garden | 15
(3 rows)
Ties: FETCH … WITH TIES
What if several rows share the last place? WITH TIES includes all rows that are equal to the last one in sort order. Three orders were placed for exactly one copy of Postgres in Practice (44.00); compare:
SELECT order_id, unit_price
FROM order_items
ORDER BY unit_price DESC
LIMIT 2;
SELECT order_id, unit_price
FROM order_items
ORDER BY unit_price DESC
FETCH FIRST 2 ROWS WITH TIES; order_id | unit_price
----------+------------
5 | 44.00
11 | 44.00
(2 rows)
order_id | unit_price
----------+------------
18 | 44.00
11 | 44.00
5 | 44.00
(3 rows)
LIMIT 2 cuts off arbitrarily between equal prices, while WITH TIES returns all three 44.00 rows.
| Syntax | Meaning |
|---|---|
LIMIT n |
at most n rows |
OFFSET m |
skip the first m rows |
LIMIT ALL |
no limit (same as leaving it out) |
FETCH FIRST n ROWS ONLY |
standard SQL version of LIMIT n |
FETCH FIRST n ROWS WITH TIES |
also include rows tied with the last one (needs ORDER BY) |
Test yourself
Which rows does ORDER BY price LIMIT 5 OFFSET 10 return?
- Rows 1–5
- Rows 10–15
- Rows 11–15 of the sorted result
- Rows 5–10
Why should LIMIT always come with ORDER BY?
- LIMIT is invalid without it
- Without an order, which rows are 'first' is undefined
- It makes queries faster
- ORDER BY is applied after LIMIT
What does FETCH FIRST 3 ROWS WITH TIES add compared with LIMIT 3?
- Nothing
- Rows that tie with the third row in sort order
- Duplicate rows
- The next three rows