LIMIT in MySQL — How It Works

What the MySQL LIMIT function does, with an example.

Published October 22, 2026

LIMIT n restricts a query to at most n rows; LIMIT offset, n skips rows first. It is used for top-N queries and pagination.

How it works

Always combine it with ORDER BY, otherwise which rows you get is undefined.

Try it in MySQL

SELECT * FROM posts ORDER BY id DESC LIMIT 10
10 newest
SELECT * FROM posts ORDER BY id LIMIT 20, 10
rows 21–30
SELECT * FROM posts LIMIT 1
one row

Practical examples

Example 1

SELECT * FROM posts ORDER BY id DESC LIMIT 10;
-- 10 newest

Example 2

SELECT * FROM posts ORDER BY id LIMIT 20, 10;
-- rows 21–30

Example 3

SELECT * FROM posts LIMIT 1;
-- one row

Use it in a real query

SELECT * FROM posts ORDER BY id DESC LIMIT 10;

Common mistakes

  • Using LIMIT without ORDER BY.
  • Deep OFFSET pagination slowing queries.
  • Confusing LIMIT 5,10 (offset 5, count 10).

Key takeaways

  • LIMIT in one line: LIMIT n restricts a query to at most n rows; LIMIT offset, n skips rows first.
  • Try queries in a test database first.
  • Check NULL handling and data types.

FAQ

Faster pagination?

Use keyset pagination: WHERE id < last_id ORDER BY id DESC LIMIT n.

Does LIMIT work in other databases?

Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.

More SQL articles