Limiting Rows: LIMIT, TOP and FETCH
Often you want only the first few rows of a result: the three most expensive courses, the latest payment, the first page of a list. SQL can stop after a set number of rows. The standard way is FETCH FIRST n ROWS ONLY; PostgreSQL, MySQL and SQLite also accept the shorter LIMIT n, and SQL Server uses TOP. Whichever you write, one rule matters more than the syntax: "first" only means something after an ORDER BY.
In One Minute
LIMIT 3or the standardFETCH FIRST 3 ROWS ONLYreturns at most three rows.- Always sort first. Without
ORDER BY, "the first three rows" can be any three rows. OFFSET 3skips three rows first; together with a limit, it shows one page at a time.- When the last place is a tie,
FETCH FIRST ... WITH TIESkeeps every row that ties. - SQL Server writes
SELECT TOP 3; older Oracle code usesROWNUM.
The Basics
SELECT columns
FROM table_name
ORDER BY columns
LIMIT n [OFFSET m];
-- the SQL standard form, which means the same:
SELECT columns
FROM table_name
ORDER BY columns
[OFFSET m ROWS] FETCH FIRST n ROWS ONLY;| Clause | Meaning |
|---|---|
LIMIT n | Return at most n rows (PostgreSQL, MySQL, SQLite) |
FETCH FIRST n ROWS ONLY | The same, in standard SQL (PostgreSQL, Oracle, SQL Server with OFFSET) |
OFFSET m | Skip the first m rows before counting |
The limit comes after the ORDER BY, at the very end of the query. Here are the three most expensive courses:
SELECT title, fee_usd
FROM courses
ORDER BY fee_usd DESC, title
LIMIT 3; title | fee_usd
------------------+---------
Java Collections | 550.00
Java OOP | 500.00
Java Basics | 450.00
(3 rows)The standard form gives the same rows:
SELECT title, fee_usd
FROM courses
ORDER BY fee_usd DESC, title
FETCH FIRST 3 ROWS ONLY; title | fee_usd
------------------+---------
Java Collections | 550.00
Java OOP | 500.00
Java Basics | 450.00
(3 rows)Always Sort First
A limit keeps the first rows of the result as it comes out. If the result isn't sorted, "first" means whatever order the database happened to produce, which can change from one run to the next as the table changes. So SELECT title FROM courses LIMIT 3 is a valid query, and it's handy for a quick peek at some rows, but it can't tell you which courses are first.
When the Last Place Is a Tie
"The three most expensive courses" hides a problem. Two courses cost $450.00, so the third place is a tie. With only fee_usd in the ORDER BY, a plain limit picks one of the two and silently drops the other:
SELECT title, fee_usd
FROM courses
ORDER BY fee_usd DESC
LIMIT 3; title | fee_usd
------------------+---------
Java Collections | 550.00
Java OOP | 500.00
Java Basics | 450.00
(3 rows)JavaScript Basics costs the same as Java Basics but isn't shown, and nothing tells you so. There are two honest fixes:
- Break the tie on purpose, with a second sort column such as
title(as in the first example), so you always get the same three rows. - Keep every tied row with
WITH TIES, which returns more than three rows when the last place is shared:
SELECT title, fee_usd
FROM courses
ORDER BY fee_usd DESC
FETCH FIRST 3 ROWS WITH TIES; title | fee_usd
-------------------+---------
Java Collections | 550.00
Java OOP | 500.00
Java Basics | 450.00
JavaScript Basics | 450.00
(4 rows)WITH TIES compares rows using every expression in the ORDER BY. Add a tie-breaker such as title, and the two $450.00 courses no longer tie, because their titles differ:
SELECT title, fee_usd
FROM courses
ORDER BY fee_usd DESC, title
FETCH FIRST 3 ROWS WITH TIES; title | fee_usd
------------------+---------
Java Collections | 550.00
Java OOP | 500.00
Java Basics | 450.00
(3 rows)So when you want to keep rows with equal fees together, sort only by fee_usd in the WITH TIES query. A tie-breaker and WITH TIES pull in opposite directions; pick the one that matches the question.
| You want | Write |
|---|---|
| At most three rows, whichever tie is picked | ORDER BY fee_usd DESC LIMIT 3 |
| Exactly three rows, always the same ones | ORDER BY fee_usd DESC, title LIMIT 3 |
| Three rows plus any that tie for third | ORDER BY fee_usd DESC FETCH FIRST 3 ROWS WITH TIES |
WITH TIES needs an ORDER BY, because ties only exist in a sorted result; without one, PostgreSQL reports WITH TIES cannot be specified without ORDER BY clause.
Skipping Rows: OFFSET and Pages
OFFSET skips rows before the limit starts counting. Apps use it to show long lists one page at a time. With three courses per page, page 1 is OFFSET 0 and page 2 is OFFSET 3:
SELECT title, fee_usd
FROM courses
ORDER BY fee_usd DESC, title
LIMIT 3 OFFSET 3; title | fee_usd
-------------------+---------
JavaScript Basics | 450.00
SQL Basics | 400.00
Git Basics | 250.00
(3 rows)The same page in standard SQL:
SELECT title, fee_usd
FROM courses
ORDER BY fee_usd DESC, title
OFFSET 3 ROWS FETCH NEXT 3 ROWS ONLY; title | fee_usd
-------------------+---------
JavaScript Basics | 450.00
SQL Basics | 400.00
Git Basics | 250.00
(3 rows)FETCH FIRST and FETCH NEXT mean the same thing, and so do ROW and ROWS; all four combinations are standard, and PostgreSQL accepts each of them. NEXT simply reads better after an OFFSET. The tie-breaker matters even more here: without it, a tied row could appear on both pages or on neither. A unique tie-breaker makes each page predictable, but only while the data stays still. If a row is added or deleted between two page requests, the rows shift, and the next page can repeat or skip one. Large offsets are also slow, because the database still reads every skipped row. Big applications often use keyset pagination instead, which starts each page right after the last row of the previous one rather than skipping rows; later lessons cover it.
In Other Databases
| Database | First 3 rows | Skip 3, then 3 |
|---|---|---|
| Standard SQL | FETCH FIRST 3 ROWS ONLY | OFFSET 3 ROWS FETCH NEXT 3 ROWS ONLY |
| PostgreSQL | LIMIT 3, or the standard form | LIMIT 3 OFFSET 3, or the standard form |
| MySQL | LIMIT 3 | LIMIT 3 OFFSET 3 |
| SQLite | LIMIT 3 | LIMIT 3 OFFSET 3 |
| SQL Server | SELECT TOP 3 ... ORDER BY ..., or ORDER BY ... OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY | ORDER BY ... OFFSET 3 ROWS FETCH NEXT 3 ROWS ONLY (OFFSET ... FETCH requires an ORDER BY) |
| Oracle | The standard form (Oracle 12c and later); older versions used ROWNUM | The standard form (12c and later) |
TOP is SQL Server only (Access uses it too), and ROWNUM is an older Oracle only way you'll still find in existing code. SQL Server's TOP 50 PERCENT and Oracle's FETCH FIRST 50 PERCENT ROWS ONLY return a share of the rows instead of a count; PostgreSQL has no PERCENT option. Tabs showing each database's version are coming to this course.
Common Mistakes
A limit without ORDER BY when you need specific rows. The query works, but the rows are arbitrary. Sort first.
Forgetting ties. "Top 3" can quietly drop a row that ties for third place. Add a tie-breaker, or use WITH TIES.
Putting LIMIT before ORDER BY. The limit goes at the very end: ... ORDER BY fee_usd DESC LIMIT 3.
Counting pages from 1 in OFFSET. Page 1 is OFFSET 0, page 2 is OFFSET 3 (with three per page): the offset is the number of rows to skip, not the page number.
Practice
Challenge: Show the two most recent payments, newest first, with their payment_id, amount_usd and paid_on. Use the standard FETCH FIRST form.
Show solution
SELECT payment_id, amount_usd, paid_on
FROM payments
ORDER BY paid_on DESC, payment_id
FETCH FIRST 2 ROWS ONLY; payment_id | amount_usd | paid_on
------------+------------+------------
5 | 200.00 | 2026-01-05
4 | 550.00 | 2025-12-29
(2 rows)Payment 5 (January 5, 2026) and payment 4 (December 29, 2025) are the latest. No two payments share a date today, but payment_id at the end guarantees a predictable order even if two ever do: a good habit for any query that keeps the first rows.
Key Takeaways
| You want to | Write |
|---|---|
| First 3 rows | ORDER BY ... LIMIT 3 |
| First 3 rows (standard) | ORDER BY ... FETCH FIRST 3 ROWS ONLY |
| Keep ties at the cut | ORDER BY ... FETCH FIRST 3 ROWS WITH TIES |
| Page 2 of 3 per page | ORDER BY ... LIMIT 3 OFFSET 3 |
Was this tutorial helpful?