SQL LIMIT and OFFSET
LIMIT caps how many rows a query returns. Combined with ORDER BY, it answers “top N” questions like the three most expensive drinks. OFFSET skips rows, which is how pagination works.
Updated December 1, 2026
Syntax
SELECT columns
FROM table_name
ORDER BY column
LIMIT row_count OFFSET rows_to_skip;
Return the first few rows
A quick LIMIT keeps exploration fast on big tables. Without ORDER BY, though, the database may return any rows it likes.
SELECT *
FROM drinks
LIMIT 3;
| id | name | category | price | calories | caffeinated |
|---|---|---|---|---|---|
| 1 | Espresso | coffee | 3 | 5 | 1 |
| 2 | Latte | coffee | 4.5 | 190 | 1 |
| 3 | Cappuccino | coffee | 4.25 | 120 | 1 |
Top N with ORDER BY
Sort first, then limit. This returns the three most expensive drinks.
SELECT name, price
FROM drinks
ORDER BY price DESC
LIMIT 3;
| name | price |
|---|---|
| Matcha Latte | 5.25 |
| Mocha | 5 |
| Cold Brew | 4.75 |
Page through results with OFFSET
OFFSET skips rows before LIMIT starts counting. With 3 rows per page, page 2 is LIMIT 3 OFFSET 3.
SELECT id, name
FROM drinks
ORDER BY id
LIMIT 3 OFFSET 3;
| id | name |
|---|---|
| 4 | Mocha |
| 5 | Cold Brew |
| 6 | Matcha Latte |
Common mistakes
- Using
LIMITwithoutORDER BYand expecting the same rows every time. - Forgetting ties: if two drinks share the third-highest price,
LIMIT 3drops one of them. Use window functions when ties matter. - Large
OFFSETvalues on huge tables are slow, because the database still reads every skipped row.
Practice LIMIT and OFFSET interactively
Write real queries in your browser and get instant feedback in the lesson “SELECT * and LIMIT” (Unit 1). It’s free.
Frequently asked questions
What is the SQL Server version of LIMIT?
SQL Server uses SELECT TOP 3 ... or OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY. Oracle and the SQL standard use FETCH FIRST 3 ROWS ONLY. PostgreSQL, MySQL and SQLite support LIMIT.
How do I get the second-highest value?
Sort descending and skip one row: ORDER BY price DESC LIMIT 1 OFFSET 1. If values can tie, use DISTINCT or DENSE_RANK instead.