Limiting the Number of Rows Returned

If you are expecting a query to still return more rows that you want, even with the filtering from a WHERE clause applied, you can add a LIMIT clause to specify the maximum number of records to be returned.

Using a LIMIT Clause

The following example retrieves all the rows from the customer_contacts table, but the LIMIT clause restricts the number of rows returned to three:

mysql> SELECT first_name, last_name
    -> FROM customer_contacts
    -> ORDER BY last_name
    -> LIMIT 3;
+------------+-----------+
| first_name | last_name |
+------------+-----------+
| Benjamin   | Britten   |
| Marie      | Curie     |
| Charles    | Darwin    |
+------------+-----------+
3 rows in set (0.00 sec)

Limiting Sorted Records

The sorting specified ...

Get Sams Teach Yourself MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.