TExcelTAD
Home › MySQL › MySQL LIMIT

MySQL LIMIT

LIMIT caps the number of rows a query returns. It goes at the very end of a SELECT:

sql
SELECT columns FROM table_name
ORDER BY ...
LIMIT row_count;
mysql · try it yourself
SELECT name, units_in_stock
FROM products
ORDER BY units_in_stock DESC
LIMIT 3;
+------------------+----------------+
| name             | units_in_stock |
+------------------+----------------+
| Whole Milk 1L    |            200 |
| Sea Salt Crisps  |            150 |
| Butter Croissant |            120 |
+------------------+----------------+

Always pair LIMIT with ORDER BY

"The first three rows" only means something if the rows have an order. Without ORDER BY the server may return any three rows, and the choice can change when data or indexes change. Make the order unique, too: if several rows tie on the sort column, add a tie-breaker such as the primary key.

Skipping rows: OFFSET

LIMIT count OFFSET skip skips skip rows and then returns up to count rows. This is the classic way to show page n of a result:

mysql · try it yourself
-- page 1, 2 and 3 of the customer list, 4 per page
SELECT customer_id, first_name FROM customers ORDER BY customer_id LIMIT 4 OFFSET 0;
SELECT customer_id, first_name FROM customers ORDER BY customer_id LIMIT 4 OFFSET 4;
SELECT customer_id, first_name FROM customers ORDER BY customer_id LIMIT 4 OFFSET 8;
+-------------+------------+
| customer_id | first_name |
+-------------+------------+
|           1 | Ana        |
|           2 | Ben        |
|           3 | Chloe      |
|           4 | Daniel     |
+-------------+------------+
+-------------+------------+
| customer_id | first_name |
+-------------+------------+
|           5 | Elena      |
|           6 | Farah      |
|           7 | George     |
|           8 | Hana       |
+-------------+------------+
+-------------+------------+
| customer_id | first_name |
+-------------+------------+
|           9 | Ivan       |
|          10 | Julia      |
|          11 | Kofi       |
|          12 | Lucia      |
+-------------+------------+

For page n (starting at 1) with size rows per page, use OFFSET (n - 1) * size.

MySQL also accepts the short form LIMIT skip, count, with the offset first. LIMIT 4, 2 means "skip 4, return 2", which surprises people who expect the opposite:

mysql · try it yourself
SELECT customer_id, first_name FROM customers ORDER BY customer_id LIMIT 4, 2;
+-------------+------------+
| customer_id | first_name |
+-------------+------------+
|           5 | Elena      |
|           6 | Farah      |
+-------------+------------+

Note: SQL Server uses SELECT TOP 3 ... and Oracle/standard SQL use FETCH FIRST 3 ROWS ONLY. MySQL only knows LIMIT.

Top-N queries

ORDER BY ... DESC LIMIT n answers "the n biggest" questions. Here are the most recent two orders:

mysql · try it yourself
SELECT order_id, customer_id, order_date
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 2;
+----------+-------------+------------+
| order_id | customer_id | order_date |
+----------+-------------+------------+
|       22 |          12 | 2025-06-03 |
|       21 |           8 | 2025-06-02 |
+----------+-------------+------------+

LIMIT in UPDATE and DELETE

MySQL allows LIMIT (and ORDER BY) on UPDATE and DELETE too. It is useful for deleting huge numbers of rows in small batches, so that each statement finishes quickly:

mysql · try it yourself
DELETE FROM order_details ORDER BY order_id, product_id LIMIT 5;
SELECT ROW_COUNT() AS deleted;
SELECT MIN(order_id) AS first_order_left FROM order_details;
+---------+
| deleted |
+---------+
|       5 |
+---------+
+------------------+
| first_order_left |
+------------------+
|                3 |
+------------------+

Deep pages are slow

LIMIT 20 OFFSET 100000 still makes the server produce and throw away 100,000 rows. For deep pagination, remember the last key you showed and continue from there ("keyset" pagination):

sql
SELECT order_id, order_date FROM orders
WHERE order_id > 120340        -- last id on the previous page
ORDER BY order_id
LIMIT 20;

Test yourself

What does LIMIT 10, 5 return?

  1. Rows 1-10
  2. Rows 6-10
  3. 5 rows after skipping 10
  4. 10 rows after skipping 5

Which OFFSET shows page 3 when each page has 20 rows?

  1. OFFSET 3
  2. OFFSET 40
  3. OFFSET 60
  4. OFFSET 20

Why should LIMIT be used together with ORDER BY?

  1. LIMIT requires it syntactically
  2. Otherwise which rows you get is not defined
  3. It makes the query faster
  4. ORDER BY is applied after LIMIT