The MySQL Limit clause is used to limit the number of rows that are returned by a MySQL select query. That is, if a particular query would normally have returned a certain number of rows, specifying LIMIT x would truncate the result set to the first x rows only.
This is a useful feature if we have a large database and do not necessarily need all the rows that are returned from a query.
The MySQL Limit rows tutorial below explains.
The syntax for LIMIT is shown below:
SELECT column_name FROM table_name LIMIT number;
Where number is the number of rows that we want in our result set.
Suppose we have our trusty "employees" table as follows:
id | first_name | last_name |
---|---|---|
1 | Paul | Pitterson |
2 | Francine | Beecham |
3 | Raul | DiNozzo |
4 | Anthony | DiNozzo |
If we wanted to return the first 2 employees, we would use the following SQL:
SELECT * FROM employees LIMIT 2;
This would yield:
id | first_name | last_name |
---|---|---|
1 | Paul | Pitterson |
2 | Francine | Beecham |
That is really all there is to the LIMIT clause in MySQL and it brings us to the end of our MySQL limit tutorial.