LearnContact
Lesson 2517 min read

LIMIT

Learn how to restrict the number of rows a query returns using LIMIT, and how to paginate results or find the top N rows.

Introduction

Real tables can hold millions of rows. Returning all of them at once is rarely necessary and can be slow — most applications only need a small "page" of results at a time, like the first 10 search results or the top 5 leaderboard entries.

The LIMIT clause restricts how many rows a query returns. Combined with an offset, it also enables pagination — showing different "pages" of the same result set.

What You Will Learn
  • The basic syntax of LIMIT.
  • How to skip rows using an offset, for pagination.
  • How to build a "page 2" style query.
  • How to combine LIMIT with ORDER BY for "top N" queries.

Basic LIMIT Syntax

LIMIT goes at the very end of a query and takes a number: the maximum number of rows to return.

SELECT column1, column2
FROM table_name
LIMIT n;

We will use the same students table, now with a few more rows so pagination has something to demonstrate.

idnameagegrademarks
1Alex16A92
2Sam17B74
3Priya16A88
4Jordan18C61
5Maya17B79
6Ravi16B70
7Lena17A95
SELECT name, marks
FROM students
LIMIT 3;
Result
+-------+-------+
| name  | marks |
+-------+-------+
| Alex  | 92    |
| Sam   | 74    |
| Priya | 88    |
+-------+-------+

Without an ORDER BY, the three rows returned are not guaranteed to be any particular three rows — which is why LIMIT is almost always paired with ORDER BY in real queries, as you will see shortly.

Pagination with OFFSET

To skip a number of rows before starting to return results, add an offset. MySQL supports two equivalent syntaxes.

-- Syntax 1: LIMIT offset, count
SELECT name, marks FROM students LIMIT 2, 3;

-- Syntax 2: LIMIT count OFFSET offset (equivalent)
SELECT name, marks FROM students LIMIT 3 OFFSET 2;

Both statements skip the first 2 rows and then return the next 3. Note that in the first syntax, the offset comes first; in the second syntax, the count comes first. Mixing them up is a common source of confusion.

Which Syntax to Use

Both forms behave identically in MySQL. Many developers prefer "LIMIT count OFFSET offset" because the word OFFSET makes the intent explicit and harder to misread.

Worked Example: Page 2

Imagine displaying students 3 per page, ordered by id. Page 1 shows the first 3 rows; page 2 should skip those 3 and show the next 3.

page_1.sql
SELECT id, name
FROM students
ORDER BY id
LIMIT 3 OFFSET 0;
Page 1 Result
+----+-------+
| id | name  |
+----+-------+
| 1  | Alex  |
| 2  | Sam   |
| 3  | Priya |
+----+-------+
page_2.sql
SELECT id, name
FROM students
ORDER BY id
LIMIT 3 OFFSET 3;
Page 2 Result
+----+--------+
| id | name   |
+----+--------+
| 4  | Jordan |
| 5  | Maya   |
| 6  | Ravi   |
+----+--------+

To reach page 2, the offset became 3 — the page size. In general, offset = pageSize * (pageNumber - 1). Note that ORDER BY id is what makes "page 2" mean the same thing every time the query runs.

Top N with ORDER BY + LIMIT

A very common pattern is finding the "top N" rows by some measure — the highest-scoring students, the most recent orders, and so on. This combines ORDER BY DESC with a small LIMIT.

top_3_students.sql
SELECT name, marks
FROM students
ORDER BY marks DESC
LIMIT 3;
Result
+-------+-------+
| name  | marks |
+-------+-------+
| Lena  | 95    |
| Alex  | 92    |
| Priya | 88    |
+-------+-------+

ORDER BY marks DESC puts the highest marks first, and LIMIT 3 keeps only the top three of those — giving exactly the three highest-scoring students in the whole table.

Common Mistakes

Avoid These Mistakes
  • Using LIMIT without ORDER BY when a specific, repeatable set of rows is expected.
  • Mixing up the two offset syntaxes and swapping the count and offset values by accident.
  • Forgetting that OFFSET counts start at 0, not 1.
  • Recomputing the wrong offset for a page — remember it is pageSize * (pageNumber - 1).

Best Practices

  • Always pair LIMIT with ORDER BY when the specific rows returned matter.
  • Prefer the explicit "LIMIT count OFFSET offset" syntax for readability.
  • Keep page size consistent across pagination requests.
  • Use LIMIT liberally during development to avoid accidentally scanning huge tables.

Frequently Asked Questions

What happens if LIMIT is larger than the number of rows in the table?

MySQL simply returns however many rows exist — no error is raised.

Is LIMIT specific to MySQL?

The keyword LIMIT is used by MySQL, PostgreSQL, and SQLite; SQL Server and some others use different syntax, such as TOP or OFFSET/FETCH.

Can LIMIT be used without OFFSET?

Yes — LIMIT n alone simply returns the first n rows the query would otherwise produce.

Why does my "top N" query return different rows each time?

This happens if ORDER BY is missing or does not fully break ties — always order by a column (or combination of columns) that uniquely determines the order you want.

Key Takeaways

  • LIMIT restricts the maximum number of rows a query returns.
  • An offset (via "LIMIT offset, count" or "LIMIT count OFFSET offset") skips rows, enabling pagination.
  • Pagination requires a consistent ORDER BY to make each page predictable.
  • ORDER BY DESC combined with LIMIT is the standard pattern for "top N" queries.

Summary

LIMIT gives you precise control over how many rows a query returns, which is essential both for pagination and for "top N" style reporting. Always combine it with ORDER BY when the specific rows returned need to be predictable.

Next, you will learn how to remove duplicate rows from a result using DISTINCT.

Lesson 25 Completed
  • You can restrict returned rows using LIMIT.
  • You can paginate results using an offset.
  • You can write a "top N" query using ORDER BY and LIMIT together.
Next Lesson →

DISTINCT