O'Reilly logo

Sams Teach Yourself MySQL by Chris Newman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required