SQLLIMITtutorialbeginner

What is LIMIT in SQL? Capping the number of rows for beginners

LIMIT caps the query result at N rows. Essential for pagination, top-N queries, and previews. We cover the syntax, OFFSET, PostgreSQL vs MySQL differences, common mistakes, and three practice exercises.

6 min readSQL · LIMIT · tutorial · beginner

LIMIT is the SQL clause that says "give me at most N rows". Simple, and incredibly useful.

Picture a table with 50,000 videos. When you open YouTube, you don't see all 50,000 — the homepage shows you 12-20 recommendations. That's LIMIT. Same goes for e-commerce pagination, game leaderboards, social-feed pages, admin previews.

Why LIMIT matters

Three classic scenarios:

  1. Top-N: top-10 sales of the week, top-3 posts by likes.
  2. Pagination: "page 2 of 100", 20 products per page.
  3. Preview / sanity check: while debugging, you want a peek at the first 5 rows to see what the data looks like.

Without LIMIT, the query returns everything. On big tables that means:

  • Slow — millions of rows over the network.
  • Pointless — the user won't see that many anyway.
  • Risky — your app may run out of memory.

Basic syntax

SELECT *
FROM table_name
LIMIT N;

Example: a streaming service videos table.

SELECT title, views
FROM videos
LIMIT 5;

Returns the first 5 rows. Which 5 — without ORDER BY, unpredictable. The database picks them "as they sit".

LIMIT almost always comes with ORDER BY

If you want "top-5 by views", LIMIT without sorting gives you 5 random rows. You have to spell out the order:

SELECT title, views
FROM videos
ORDER BY views DESC
LIMIT 5;

Now it's "the 5 most-viewed videos". The database sorts first, then truncates.

Rule of thumb: LIMIT without ORDER BY only makes sense in three cases — preview during dev, picking a random row (ORDER BY RANDOM()), debugging. Everywhere else — write ORDER BY.

Example: streaming service

idtitleauthorviewslikes
1Learn SQL in 30 minutesCode Academy25000012000
2Cats compile codeFunny Devs120000095000
3JOINs explained simplyDB Guru800004500
4A coder's borscht recipeTech Cooks300001800
510 mistakes in SELECTDB Guru1500008900
6Top React hooks 2024Web Dev50000025000
7A short history of LinuxOS Lessons900005500

Query: top 3 by views.

SELECT title, views
FROM videos
ORDER BY views DESC
LIMIT 3;

Result:

titleviews
Cats compile code1200000
Top React hooks 2024500000
Learn SQL in 30 min250000

Seven rows in, three rows out. The ones with the highest views.

OFFSET — skip the first N rows

The partner clause to LIMIT is OFFSET. It skips N rows before starting to return.

SELECT title, views
FROM videos
ORDER BY views DESC
LIMIT 3 OFFSET 3;

Reads as: "sort by views descending, skip 3 rows, return the next 3".

Result for our table (positions 4-6 by views):

titleviews
Learn SQL in 30 min250000
10 mistakes in SELECT150000
A history of Linux90000

That's pagination. Page 1 — LIMIT 3 OFFSET 0. Page 2 — LIMIT 3 OFFSET 3. Page 3 — LIMIT 3 OFFSET 6.

The pagination formula:

LIMIT page_size OFFSET (page_number - 1) * page_size

PostgreSQL vs MySQL — almost identical

Both understand LIMIT N and LIMIT N OFFSET M. Quick differences:

  • MySQL also supports a shorthand LIMIT M, N — skip M, return N. Considered legacy; use LIMIT N OFFSET M instead.
  • PostgreSQL also speaks the SQL-standard form: FETCH FIRST 10 ROWS ONLY (instead of LIMIT 10) and OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY. Verbose, but part of SQL:2008. In practice everyone just writes LIMIT/OFFSET.

For beginners: remember LIMIT N OFFSET M — works in both.

Bigger example: e-commerce pagination

products table:

idnamecategorypricepopularity
1iPhone 15Electronics999980
2Bosch kettleAppliances49320
3Book "Clean Code"Books22850
4DeLonghi coffee makerAppliances380410
5MacBook AirElectronics1450920
6Book "SQL in a Month"Books17670
7Pixel 8Electronics830740
8LG fridgeAppliances720280
9Slow cookerAppliances89560
10Book "JavaScript"Books20510

Goal: show page 2 (4 products per page), sorted by popularity.

SELECT id, name, popularity
FROM products
ORDER BY popularity DESC
LIMIT 4 OFFSET 4;

Result:

idnamepopularity
7Pixel 8740
6Book "SQL in a Month"670
9Slow cooker560
10Book "JavaScript"510

Page 1 (most popular): iPhone 15, MacBook Air, Book "Clean Code", Pixel 8. Page 2: rows 5-8 by popularity. Page 3 would be OFFSET 8.

Random sampling

Combo for a random row (handy for daily challenges, feature-of-the-day, etc.):

-- PostgreSQL
SELECT * FROM products ORDER BY RANDOM() LIMIT 1;

-- MySQL
SELECT * FROM products ORDER BY RAND() LIMIT 1;

Heads-up: on big tables RANDOM() / RAND() is heavy — the database has to generate a random number per row and sort. Fine for small tables, painful for millions of rows. For those, use other tricks (e.g. WHERE id = ROUND(RANDOM() * (SELECT MAX(id) FROM products))).

Common beginner mistakes

1. LIMIT without ORDER BY, expecting an order. People assume "the database returns in insertion order". It doesn't. Need an order? Write ORDER BY.

2. Pagination without ORDER BY. The sneakiest bug. Without sorting, page 2 may contain rows from page 1, and the user sees duplicates. Always sort by a unique column (typically id).

3. Huge OFFSETs. OFFSET 1000000 forces the database to generate a million rows and then throw them away. Slow. For big tables prefer keyset pagination: WHERE id > last_seen_id LIMIT N.

4. LIMIT with UNION. If you want LIMIT to apply to the whole UNION, parenthesise or put ORDER BY/LIMIT at the very end:

(SELECT name FROM authors UNION SELECT name FROM editors)
ORDER BY name
LIMIT 10;

5. Negative LIMIT or LIMIT 0. LIMIT 0 is valid — returns 0 rows (occasionally useful for sanity-checking a query without fetching data). Negative numbers throw an error.

6. LIMIT in UPDATE/DELETE — DB-specific. MySQL allows UPDATE … LIMIT 100. PostgreSQL doesn't — you have to use a subquery or CTE.

Quick recap

  • LIMIT N — return at most N rows.
  • OFFSET M — skip M rows before starting.
  • Almost always paired with ORDER BY — otherwise the order is unpredictable.
  • Used for top-N, pagination, previews.
  • Same syntax in PostgreSQL and MySQL.
  • Huge OFFSETs are slow — prefer keyset pagination on big tables.

Try it yourself

On the videos table:

  1. Get the top 5 videos by likes.
  2. Get page 2 (3 videos per page), sorted by views descending.
  3. Pick a random video for "video of the day".

Write the queries on your own. Once you're comfortable with the ORDER BY + LIMIT combo, half of all real-world read queries reduce to this pattern.

Practice on real tasks

Solve tasks in the SQL trainer with instant grading and hints.

Open trainer