Limit the Number of Resulting Records

Learn how to retrieve required rows and limit them.

We'll cover the following

Sometimes, we want to return the first row that matches the select criteria, even if more rows might match. We can do that using the limit at the end of the select statement. For example, let’s suppose that we want to get the most expensive product in our catalog. This is returned with the following select statement:

1 mysql> select * from products order by price desc limit 1;
2 +----+-----------------------------+-------+
3 | id | name                        | price | 
4 +----+-----------------------------+-------+
5 |  1 | Game of Thrones-S01-DVD.    | 50.00 |
6 +----+-----------------------------+-------+
7 1 row in set(0.00 sec)

Let’s verify it by executing it:

Get hands-on with 1400+ tech skills courses.