Learn SQL
-- filtering.limit_offset

LIMIT & OFFSET — Top-N and Paging in SQL

Cap the result, skip ahead page by page

--concept

LIMIT caps how many rows come back; OFFSET skips rows before returning — together they power “top 3 earners” lists and page-by-page results. They always ride on ORDER BY: a top-N without a sort is a random N. Dialect note: MySQL/PostgreSQL/SQLite use LIMIT…OFFSET, SQL Server uses TOP or OFFSET-FETCH.

--how it works
1

Sort first, always

LIMIT without ORDER BY returns an arbitrary N — the sort decides which rows deserve to be first.

2

Cap with LIMIT

LIMIT 3 keeps at most three rows after sorting.

3

Skip with OFFSET

OFFSET 3 discards the first three rows — page 2 begins where page 1 ended.

4

Page arithmetic

Page p of size s is LIMIT s OFFSET (p−1)·s — page 3 of 5 uses OFFSET 10.

5

Know the dialect

SQL Server spells it TOP n or OFFSET … FETCH FIRST n ROWS ONLY; MySQL/PostgreSQL/SQLite use LIMIT…OFFSET.

--visualization
Cynthia91,000✓ pageAlice85,000✓ pageFarid73,000✓ pageElena58,000✕ beyond LIMITBob52,000✕ beyond LIMITDavid47,000✕ beyond LIMITORDER BY salary DESC LIMIT 3 OFFSET 0
returned offset-skipped beyond LIMIT
Stage 1 / 8
ORDER BY salary DESC · LIMIT 3 OFFSET 0 — skip 0, then take up to 3.
--example
query.sql
1SELECT name, salary
2FROM employees
3ORDER BY salary DESC
4LIMIT 3 OFFSET 0;
Result
namesalary
Cynthia91000
Alice85000
Farid73000

The podium: three highest-paid employees. Change OFFSET to 3 and the second-page trio appears instead. Drag the sliders in the visualization.

--variants

Top-N

The podium query — sort, then cap.

query.sql
1SELECT name, salary
2FROM employees
3ORDER BY salary DESC
4LIMIT 3;
Result
namesalary
Cynthia91000
Alice85000
Farid73000
1All six are sorted first; only then does LIMIT cut.
2Rows 4–6 are discarded after the sort, not before.
3This is the exact shape of every leaderboard query.

Paging with OFFSET

Page 2 of the same sorted list.

query.sql
1SELECT name, salary
2FROM employees
3ORDER BY salary DESC
4LIMIT 3 OFFSET 3;
Result
namesalary
Elena58000
Bob52000
David47000
1The sort runs identically; OFFSET drops the first three rows.
2What remains is the second page of three.
3OFFSET 6 would return an empty table — past the end.

A short last page

The final page may hold fewer rows than the page size.

query.sql
1SELECT name, salary
2FROM employees
3ORDER BY salary DESC
4LIMIT 4 OFFSET 3;
Result
namesalary
Elena58000
Bob52000
David47000
1Only three rows remain after skipping three — LIMIT 4 cannot invent a fourth.
2Engines return what exists; no error, no padding.
3UIs use the short page as the "no more results" signal.

LIMIT without ORDER BY

A bug in waiting — the N you get is arbitrary.

query.sql
1SELECT name, salary FROM employees LIMIT 3;
2-- which 3? undefined!
Result
namesalary
Alice85000
Bob52000
Cynthia91000
1The engine is free to stop at any three rows.
2It happens to look sensible here; on a bigger table it would not.
3Rule: LIMIT rides on ORDER BY or it lies.
--key facts
DialectsMySQL/PG/SQLite: LIMIT·OFFSET — SQL Server: TOP / OFFSET-FETCH
RuleLIMIT without ORDER BY returns an arbitrary top-N
ScaleOFFSET 100000 makes engines walk 100k rows — prefer keyset filters
--use cases
Leaderboards: top 3, top 10, "highest paid per department"
Infinite-scroll and numbered pages in every web app
Sampling during development: LIMIT 20 to peek at a table
Cost control: capping what a bad query can return to the UI
--under the hood
Top-N with a matching index can short-circuit: the engine keeps only the best N rows seen.
Deep OFFSET (page 5000) walks all skipped rows — keyset pagination (WHERE salary < :last_seen … LIMIT n) stays fast forever.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.