The limit Clause
In some situations, you may not be interested in all of the rows returned by a query. For example, you might construct a query that returns all of the bank tellers along with the number of accounts opened by each teller. If your reason for executing the query is to determine the top three tellers so that they can receive an award from the bank, then you dont necessarily need to know who came in fourth, fifth, and so on. To help with these types of situations, MySQLs select
statement includes the limit
clause, which allows you to restrict the number of rows returned by a query.
To demonstrate the utility of the limit
clause, I will begin by constructing a query to show the number of accounts opened by each bank teller:
mysql>
SELECT open_emp_id, COUNT(*) how_many
->
FROM account
->
GROUP BY open_emp_id;
+-------------+----------+| open_emp_id | how_many |+-------------+----------+| 1 | 8 || 10 | 7 || 13 | 3 || 16 | 6 |+-------------+----------+4 rows in set (0.31 sec)
The results show that four different tellers opened accounts; if you want to limit the result set to only three records, you can add a limit
clause specifying that only three records be returned:
mysql>
SELECT open_emp_id, COUNT(*) how_many
->
FROM account
->
GROUP BY open_emp_id
->
LIMIT 3;
+-------------+----------+| open_emp_id | how_many |+-------------+----------+| 1 | 8 || 10 | 7 || 13 | 3 |+-------------+----------+3 rows in set (0.06 sec)
Thanks to the limit
clause (the fourth line of the query), the result set now includes exactly three records, and the fourth teller (employee ID 16) has been discarded from the result set.
Combining the limit clause with the order by clause
While the previous query returns three records, theres one small problem; you havent described which three of the four records you are interested in. If you are looking for three specific records, such as the three tellers who opened the most accounts, you will need to use the limit
clause in concert with an order by
clause, as in:
mysql>
SELECT open_emp_id, COUNT(*) how_many
->
FROM account
->
GROUP BY open_emp_id
->
ORDER BY how_many DESC
->
LIMIT 3;
+-------------+----------+| open_emp_id | how_many |+-------------+----------+| 1 | 8 || 10 | 7 || 16 | 6 |+-------------+----------+3 rows in set (0.03 sec)
The difference between this query and the previous query is that the limit
clause is now being applied to an ordered set, resulting in the three tellers with the most opened accounts being included in the final result set. Unless you are interested in seeing only an arbitrary sample of records, you will generally want to use an order by
clause along with a limit
clause.
Note
The limit
clause is applied after all filtering, grouping, and ordering have occurred, so it will never change the outcome of your select
statement other than restricting the number of records returned by the statement .
The limit clauses optional second parameter
Instead of finding the top three tellers, lets say your goal is to identify all but the top two tellers (instead of giving awards to top performers, the bank will be sending some of the less-productive tellers to assertiveness training). For these types of situations, the limit
clause allows for an optional second parameter; when two parameters are used, the first designates at which record to begin adding records to the final result set, and the second designates how many records to include. When specifying a record by number, remember that MySQL designates the first record as record 0. Therefore, if your goal is to find the third-best performer, you can do the following:
mysql>
SELECT open_emp_id, COUNT(*) how_many
->
FROM account
->
GROUP BY open_emp_id
->
ORDER BY how_many DESC
->
LIMIT 2, 1;
+-------------+----------+| open_emp_id | how_many |+-------------+----------+| 16 | 6 |+-------------+----------+1 row in set (0.00 sec)
In this example, the zeroth and first records are discarded, and records are included starting at the second record. Since the second parameter in the limit
clause is 1
, only a single record is included.
If you want to start at the second position and include all the remaining records, you can make the second argument to the limit
clause large enough to guarantee that all remaining records are included. If you do not know how many tellers opened new accounts, therefore, you might do something like the following to find all but the top two performers:
mysql>
SELECT open_emp_id, COUNT(*) how_many
->
FROM account
->
GROUP BY open_emp_id
->
ORDER BY how_many DESC
->
LIMIT 2, 999999999;
+-------------+----------+| open_emp_id | how_many |+-------------+----------+| 16 | 6 || 13 | 3 |+-------------+----------+2 rows in set (0.00 sec)