Concept · Basic queries

LIMIT

LIMIT caps the number of rows in the result. Add ORDER BY to decide which rows come first, plus a key to break ties.

LIMIT example

SQL
SELECT id,
       name,
       price
FROM products
ORDER BY price DESC, id ASC
LIMIT 3;

The rows are sorted from highest to lowest first, and then the top three are kept. id ASC provides a stable tiebreaker when two products share a price.

What to look for: If the sorted prices are 90, 70, 70 and 50, LIMIT 3 returns three products, not every product tied for third place. The id added to the sort decides which one comes first.

Check the result with small data

We want the two highest marks, not just any two rows.

Self-contained query · SQLite
WITH notes(name, note) AS (
  VALUES ('Amy', 9), ('Liam', 7), ('Eva', 10)
)
SELECT name, note FROM notes
ORDER BY note DESC, name ASC LIMIT 2;
Query result
namenote
Eva10
Amy9

ORDER BY decides which rows come first; LIMIT 2 then trims the list. Without a defined order, the two rows returned could change.

Check your understanding: Which mark is left out, and why?

Show answer

Liam's, with 7. Sorting from highest to lowest puts Eva (10) and Amy (9) first, and LIMIT 2 cuts the list there.

How LIMIT is used

LIMIT 1 returns at most one row; LIMIT 0 returns none. If the table has fewer rows than the limit, the result is simply shorter.

Common mix-ups with LIMIT

Confusing a limit with a filter

LIMIT 3 does not select rows that meet a condition; it only trims the result. Use WHERE to decide which rows are candidates.

Asking for a top N without sorting

The query may return three rows, but you cannot claim they are the highest or lowest values.

Key differences between engines
  • PostgreSQL, SQLite and MySQL: accept LIMIT.
  • SQL Server: offers TOP for simple limits and OFFSET ... FETCH for pagination.
  • Standard SQL: uses FETCH FIRST ... ROWS ONLY, with availability and details depending on the engine.

LIMIT without ORDER BY returns arbitrary rows

LIMIT restricts how many rows a query returns. It is handy for previewing data, but on its own it does not say which rows: without ORDER BY, the engine returns whichever rows it finds first, and those can change after an update or under a different execution plan.

To get “the top N”, define the criterion and break ties. If two products tie for the last place, LIMIT 2 needs to know which one to include, so add a second column to the sort. If you need to include every tied row, LIMIT is not enough; use a window function or a query that compares against the cut-off value.

The syntax depends on the engine: LIMIT in PostgreSQL, MySQL and SQLite, TOP in SQL Server and FETCH FIRST n ROWS ONLY in standard SQL and in Oracle since version 12c. Test the limit with a tie right at the cut-off to confirm the result.

Self-contained case to check in SQLite

Two products tie at 80 and we only want two rows; the name breaks the tie.

Data and query · SQLite
WITH sales(product, amount) AS (
  VALUES ('A', 50), ('B', 80), ('C', 80), ('D', 20)
)
SELECT product, amount
FROM sales
ORDER BY amount DESC, product
LIMIT 2;
Verified result of the self-contained case
productamount
B80
C80

Both tie at 80 and both make it in. If there were a third product at 80, the tiebreaker would decide which one was left out.

Check your interpretation: Which two products would the query return if we added E at 90?

Show answer

E and B: E (90) comes first and, of the two tied at 80, B gets in because the tiebreak by product places it before C.

Technical questions: PostgreSQL: LIMIT and OFFSET (opens in a new tab).