Sometimes we have to fetch a certain number of records not all.MySQL provides this feature where we do not need to fetch all the data from the table. we are talking about the limit in MySQL
We can use this feature by using LIMIT in MySQL. LIMIT provides the functionality where we can fetch the desired number of records. Let’s see how we can use the limit in MySQL.
Syntax of limit
<Select statement> limit number_of_record_to_be_fetched;
- <Select statement> is the select statement.
- limit is the clause in MySQL
number_of_record_to_be_fetched
is the limit of the number to be fetched.
Example of LIMIT
Let’s understand the MySQL limit by taking an example:
We have the employee table having the following data
mysql> SELECT * FROM employee;
+-------+-----------+----------+-------------------+
| EMPID | FIRSTNAME | LASTNAME | DESIGNATION |
+-------+-----------+----------+-------------------+
| 100 | Ronny | Waugh | Accountant |
| 101 | Jack | Low | Sr. Accountant |
| 102 | Peter | Step | Advocate |
| 103 | hallo | sit | clerk |
| 104 | mike | clow | clerk |
| 105 | jame | right | Software Engineer |
+-------+-----------+----------+-------------------+
Let’s select few records by using the limit clause of MySQL.
mysql> SELECT * FROM employee limit 2;
+-------+-----------+----------+----------------+
| EMPID | FIRSTNAME | LASTNAME | DESIGNATION |
+-------+-----------+----------+----------------+
| 100 | Ronny | Waugh | Accountant |
| 101 | Jack | Low | Sr. Accountant |
+-------+-----------+----------+----------------+
2 rows in set (0.00 sec)
We have put limit 2 to fetch only 2 records.