LIMIT, OFFSET and FETCH FIRST
the first n rows, WITH TIES, and why LIMIT without ORDER BY is a coin toss.
- address
- everything.basics.limit_and_offset
- part
- part 2, one table
- chapter
- chapter 5 of 12
- reading time
- 8 min
LIMIT n keeps the first n rows of the result, in whatever order the result has. with no ORDER BY that's no order at all, so "the first 3" can be any 3. OFFSET n skips rows, and to skip them it has to read them.
FETCH FIRST n ROWS is the standard spelling, and its WITH TIES keeps going while rows tie with the last one.
the picture
you know how LIMIT 10 feels like "the first ten"? first by what? without an ORDER BY there's no first. there's only the order postgres happened to find the rows in, which is something it never promised to keep.
think of asking for "the first three people in the room". in a queue, that means something. in a crowd, it means whoever is nearest the door right now, and that changes when someone wanders off and comes back. a table is a crowd.
- 1SELECT id, name, year
- 2FROM students
- 3LIMIT 3;
| idint4 | nametext | yearint2 |
|---|---|---|
| 1 | Maya Chen | 4 |
| 2 | Liam Walsh | 3 |
| 3 | Aisha Bello | 2 |
no ORDER BY: postgres reads the table front to back and stops after three. today that's students 1, 2 and 3.
here it is in your browser. the second block runs in the same session, after the first:
SELECT id, name, year
FROM students
LIMIT 3;| idint4 | nametext | yearint2 |
|---|---|---|
| 1 | Maya Chen | 4 |
| 2 | Liam Walsh | 3 |
| 3 | Aisha Bello | 2 |
UPDATE students SET year = year WHERE id = 1;
SELECT id, name, year
FROM students
LIMIT 3;| idint4 | nametext | yearint2 |
|---|---|---|
| 2 | Liam Walsh | 3 |
| 3 | Aisha Bello | 2 |
| 4 | Noah Schmidt | 1 |
students 1, 2, 3, then 2, 3, 4. nothing is broken: postgres never overwrites a row, it writes a new version, and a sequential scan returns rows in the order they sit on the page. on a real server, a parallel plan can shuffle the answer without anyone touching the table at all, and so can synchronize_seqscans, which lets a scan of a big table start halfway through to share reads with a scan that's already running. (first query promised you'd see this go wrong. there it is.)
LIMIT goes last
in the logical order, LIMIT is the very last step: it cuts the finished, sorted result. so ORDER BY ... LIMIT n means exactly what it says, as long as the ORDER BY decides every position. when the sort leaves ties, LIMIT cuts through the middle of a tie, and which of the tied rows make it is up to the sort (ties shows postgres picking differently with and without the LIMIT).
the fix is the same as ever: break the tie with a unique key, so the cut lands in one place. or, when the tie is the point, keep all of it.
FETCH FIRST and WITH TIES
the standard's spelling of LIMIT is FETCH FIRST n ROWS ONLY (SQL:2008, and OFFSET n ROWS for the offset). postgres accepts both, and they do the same thing:
SELECT code FROM courses ORDER BY code LIMIT 3;
SELECT code FROM courses ORDER BY code FETCH FIRST 3 ROWS ONLY;| codetext |
|---|
| CS101 |
| CS201 |
| CS301 |
| codetext |
|---|
| CS101 |
| CS201 |
| CS301 |
FETCH FIRST has one trick LIMIT doesn't: WITH TIES (postgres 13 and up). instead of stopping at exactly n rows, it keeps going as long as the next row ties with the last one it kept, on the ORDER BY keys.
SELECT code, credits, title
FROM courses
ORDER BY credits DESC
FETCH FIRST 3 ROWS WITH TIES;- 1SELECT code, credits, title
- 2FROM courses
- 3ORDER BY credits DESC
- 4FETCH FIRST 3 ROWS WITH TIES;
| codetext | creditsint2 | titletext |
|---|---|---|
| CS101 | 4 | Intro to Programming |
| CS201 | 4 | Data Structures |
| CS301 | 4 | Databases |
| CS350 | 4 | Operating Systems |
| CS401 | 4 | Database Internals |
| CS499 | 6 | Senior Thesis |
| HIST101 | 3 | History of Computing |
| MATH101 | 4 | Calculus I |
| MATH201 | 3 | Discrete Math |
| MATH301 | 3 | Linear Algebra |
| PHYS101 | 4 | Mechanics |
| PHYS201 | 4 | Electromagnetism |
FROM hands over all 12 courses, in no particular order.
the query that produced this table
asked for 3, got 9. that's the honest answer to "the courses with the most credits, three or so": it doesn't arbitrarily drop five courses that are exactly as good as the ones it kept. it needs an ORDER BY to know what a tie is, and says so if you forget:
SELECT code FROM courses FETCH FIRST 3 ROWS WITH TIES;OFFSET reads what it skips
LIMIT 10 OFFSET 1000 is page 101 of a list. it looks like a jump. it's a walk: postgres produces rows in order and throws the first 1,000 away. there's no way to land on row 1,001 without counting past the thousand before it.
WHERE id > 9990, and reads 10.the plan for the last page says it plainly. the Limit hands back 10 rows, the index scan under it produced 10,000:
EXPLAIN (ANALYZE, COSTS OFF, BUFFERS OFF, TIMING OFF, SUMMARY OFF)
SELECT id, customer_id, status FROM orders ORDER BY id LIMIT 10 OFFSET 9990;on 10,000 orders that's nothing. on 10 million it's the slow page every app has. the fix is to remember where the last page ended and ask for what comes after it, WHERE id > 9990 ORDER BY id LIMIT 10, which an index can jump straight to. that's keyset pagination, and it's the right default for "load more".
pages that move
OFFSET has a second problem, and it doesn't need a big table. the offset counts rows at the moment you ask. if rows arrive between page 1 and page 2, everything slides down, and page 2 starts with something you've already seen:
- 1SELECT id, customer_id, status, ordered_at
- 2FROM orders
- 3ORDER BY ordered_at DESC, id DESC
- 4LIMIT 5;
| idint4 | customer_idint4 | statustext | ordered_attimestamptz |
|---|---|---|---|
| 10000 | 399 | placed | 2026-06-27 03:00:00+00 |
| 9999 | 1267 | placed | 2026-06-27 02:13:00+00 |
| 9998 | 417 | placed | 2026-06-27 01:04:00+00 |
| 9997 | 1094 | placed | 2026-06-27 00:31:00+00 |
| 9996 | 974 | placed | 2026-06-26 23:44:00+00 |
page 1 of the newest orders, five to a page.
keyset pagination doesn't have this problem either: "orders older than the last one I showed you" means the same thing no matter what got inserted at the top.
one level deeperLIMIT ALL, LIMIT NULL, and OFFSET on its own
LIMIT ALL and LIMIT NULL both mean no limit, which is handy when the limit is a parameter that's sometimes "everything". OFFSET works without a LIMIT, and both accept expressions, not just numbers:
SELECT code FROM courses ORDER BY code LIMIT NULL OFFSET 10;| codetext |
|---|
| PHYS101 |
| PHYS201 |
the last 2 of 12. and LIMIT doesn't make the work smaller by itself: if postgres can't produce rows in order without sorting all of them, it sorts all of them (it just keeps fewer, see the top-N heapsort in sorting with ORDER BY). an index on the ORDER BY columns is what lets it stop early, like the index scan above that read exactly 10 rows for page 1.
keylines
- LIMIT keeps the first n rows of the result's order. no ORDER BY, no first.
- a row can move just because it was updated, so "the first 3" can change with no visible change.
- LIMIT cuts through ties at random. break them with a unique key, or keep them with WITH TIES.
- OFFSET reads every row it skips, and shifts when rows come and go.
- for deep pages and "load more", remember the last key instead: keyset pagination.