TExcelTAD
Home › SQL › SQL LIMIT / TOP

SQL LIMIT / TOP

LIMIT restricts how many rows a query returns. It is the tool for "top 5" lists and for showing results page by page. Here the dialects really differ — see the table below — but the idea is the same everywhere.

sql
SELECT columns
FROM table_name
ORDER BY ...
LIMIT number;

The top three

The three most expensive products:

sql · try it yourself
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;
name                        | price
----------------------------+------
Noise-Cancelling Headphones | 149
Mechanical Keyboard         | 89
Coffee Grinder              | 59.9
(3 rows)

Note: LIMIT without ORDER BY gives you some rows, not the "first" ones — a table has no natural order. Always combine them.

Skipping rows with OFFSET

OFFSET n skips the first n rows. Together with LIMIT it gives pagination. Page 2 of the product list, 5 products per page:

sql · try it yourself
SELECT product_id, name
FROM products
ORDER BY product_id
LIMIT 5 OFFSET 5;
product_id | name
-----------+-----------------
6          | Sourdough Loaf
7          | Butter Croissant
8          | Chocolate Cake
9          | Bagels
10         | Whole Milk
(5 rows)

For page p (starting at 1) with n rows per page, use LIMIT n OFFSET (p - 1) * n. SQLite and MySQL also accept the short form LIMIT 5, 5 (offset first!), which is easy to misread.

Different databases, different syntax

Database Top 3 by price
SQLite, MySQL, PostgreSQL SELECT … ORDER BY price DESC LIMIT 3
SQL Server, MS Access SELECT TOP 3 … ORDER BY price DESC
Standard SQL, Oracle 12c+, PostgreSQL, SQL Server 2012+ … ORDER BY price DESC FETCH FIRST 3 ROWS ONLY
Oracle (old) WHERE ROWNUM <= 3 (applied before ORDER BY!)

Paging in SQL Server and standard SQL:

sql
SELECT product_id, name
FROM products
ORDER BY product_id
OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY;

SQL Server also has SELECT TOP 10 PERCENT … and TOP 3 WITH TIES, which includes extra rows that tie with the last one.

Ties

With LIMIT, rows that tie on the sort key with the last row are cut off arbitrarily. If several rows can share the same value, add a unique column (such as the id) as a tie-breaker so the result is always the same:

sql · try it yourself
SELECT name, joined
FROM customers
ORDER BY joined, customer_id
LIMIT 4;
name           | joined
---------------+-----------
Corner Cafe    | 2024-01-01
Priya Kowalski | 2024-01-04
Elif Yilmaz    | 2024-01-07
Bright Bytes   | 2024-02-08
(4 rows)

Combining WHERE, ORDER BY and LIMIT

The clauses are evaluated in this order: WHERE filters, ORDER BY sorts, LIMIT cuts. The two most recent delivered orders:

sql · try it yourself
SELECT order_id, customer_id, order_date
FROM orders
WHERE status = 'delivered'
ORDER BY order_date DESC
LIMIT 2;
order_id | customer_id | order_date
---------+-------------+-----------
33       | 9           | 2025-09-07
32       | 21          | 2025-08-14
(2 rows)

Tip: Large OFFSET values are slow, because the database still produces and skips all earlier rows. For deep pagination, remember the last id you showed and filter with WHERE product_id > :last_id ORDER BY product_id LIMIT 5 ("keyset pagination").

Test yourself

Which SQL Server syntax returns the first 5 rows?

  1. LIMIT 5
  2. SELECT TOP 5 ...
  3. ROWNUM 5
  4. FIRST 5

Which rows does LIMIT 10 OFFSET 20 return?

  1. Rows 10–20
  2. Rows 21–30
  3. Rows 20–30
  4. Rows 1–10 of page 20

Why should LIMIT be combined with ORDER BY?

  1. LIMIT requires it syntactically
  2. Without it, which rows you get is undefined
  3. It makes the query faster
  4. It removes duplicates