What is the use of the LIMIT keyword in MySQL query?

MySQLMySQLi Database

LIMIT keyword in MySQL can specify the number of records to be returned in the output. LIMIT clause restricts the number of rows to be returned. It can be understood with the help of the following example −

Example

mysql> Select * from Student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
|  101 | YashPal | Amritsar   | History    |
|  105 | Gaurav  | Chandigarh | Literature |
|  125 | Raman   | Shimla     | Computers  |
|  130 | Ram     | Jhansi     | Computers  |
|  132 | Shyam   | Chandigarh | Economics  |
|  133 | Mohan   | Delhi      | Computers  |
|  150 | Saurabh | NULL       | Literature |
+------+---------+------------+------------+
7 rows in set (0.00 sec)

The above result set shows that the table ‘student_info’ is having totaled 7 rows.

But, if we want to get only the top 2 rows in output then we can use LIMIT keyword followed by 2 as follows −

mysql> Select * from Student_info LIMIT 2;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
|  101 | YashPal | Amritsar   | History    |
|  105 | Gaurav  | Chandigarh | Literature |
+------+---------+------------+------------+
2 rows in set (0.00 sec)
raja
Published on 22-Feb-2018 07:04:30
Advertisements