Stop 02 · Basic SELECT QueriesTopics · 8/8

LIMIT, OFFSET and FETCH FIRST

Part of the SQL Roadmap

In short

Nobody wants all 2 lakh orders on screen. Top-10 lists and "page 3 of results" are everywhere, and they come from limiting and skipping rows after sorting.

LIMIT n returns only the first n rows and OFFSET skips rows, which is how you get top-N lists and simple pagination. The standard form is OFFSET m ROWS FETCH FIRST n ROWS ONLY, and SQL Server uses TOP.

The jaadu flips through a thick restaurant menu, holding a few pages back with one hand while reading the next page.

Think of it like this

Reading a thick menu page by page

A big restaurant menu shows 10 dishes per page. Page 1 is dishes 1 to 10. For page 3 you flip past 20 dishes and read the next 10. LIMIT is how many dishes fit on a page, OFFSET is how many you flip past.

Watch it happen

Page 2 with ORDER BY amount DESC LIMIT 3 OFFSET 3

9800
0
7500
1
6200
2
5400
3
4100
4
3900
5
2500
6
1200
7

1/5First the rows are sorted by amount, largest first. Paging only makes sense on sorted rows.

The big ideas

What you need to know

LIMIT caps the rows

LIMIT 10 returns at most ten rows. Works in PostgreSQL, MySQL and SQLite.

OFFSET skips rows

OFFSET 20 skips the first twenty. Combined with LIMIT, that's pagination.

FETCH FIRST is the standard

OFFSET 20 ROWS FETCH FIRST 10 ROWS ONLY is ANSI SQL, used by PostgreSQL, Oracle and SQL Server. SQL Server also has TOP.

Big offsets get slow

OFFSET 100000 still reads and throws away 100,000 rows. Deep pages use keyset paging (WHERE id < last_seen_id) instead.

See it in code

Walk through the code

sql
1-- Top 10 biggest orders2SELECT order_id, customer_id, amount3FROM orders4ORDER BY amount DESC, order_id5LIMIT 10;6 7-- Page 3, 10 rows per page (rows 21 to 30)8SELECT order_id, customer_id, amount9FROM orders10ORDER BY amount DESC, order_id11LIMIT 10 OFFSET 20;12 13-- Same page, standard syntax14SELECT order_id, customer_id, amount15FROM orders16ORDER BY amount DESC, order_id17OFFSET 20 ROWS FETCH FIRST 10 ROWS ONLY;

1/3Sort first, then keep ten. order_id breaks ties so the order is stable.

All three run in PostgreSQL. MySQL supports only the LIMIT forms. SQL Server needs the standard OFFSET ... FETCH form or SELECT TOP 10.

Save yourself the pain

Common mistakes

Oops

Using LIMIT without ORDER BY to get "the top 10".

Do this instead

Without ORDER BY you get 10 random-ish rows. Always sort first.

Oops

Sorting on a column with ties, so rows jump between pages.

Do this instead

Add a unique tie-breaker like order_id to the ORDER BY.

Oops

Calculating page 3 as OFFSET 30 with 10 rows per page.

Do this instead

OFFSET = (page - 1) × size, so page 3 is OFFSET 20.

Your mission

Build pagination

~20 min

Quick check

With 20 rows per page, which clause gets page 4?

Go deeper

  1. 1Get the five most expensive products using ORDER BY with LIMIT.
  2. 2Build page 3 of a 10-rows-per-page list using LIMIT and OFFSET.
  3. 3Learn why large OFFSET values get slow and how keyset pagination (WHERE id > last_seen_id) avoids it.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge