Why is “LIMIT 0” even allowed in MySQL SELECT statements?

MySQLMySQLi Database

As you know if you use LIMIT 0 in MySQL SELECT statement, it returns an empty set.

The LIMIT can be used when you want a specified number of rows from a result rather than the entire rows. If you use any MySQL API, then the job of LIMIT is to acquire the type of result columns.

LIMIT 0 can be used to check the validity of a query. For more details use the following link −

https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html

Here is the demo of LIMIT 0. The query to create a table is as follows −

mysql> create table Limit0Demo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.61 sec)

Now you can insert some records in the table using insert command. The query is as follows −

mysql> insert into Limit0Demo(Name) values('David');
Query OK, 1 row affected (0.13 sec)
mysql> insert into Limit0Demo(Name) values('Larry');
Query OK, 1 row affected (0.24 sec)
mysql> insert into Limit0Demo(Name) values('Carol');
Query OK, 1 row affected (0.17 sec)
mysql> insert into Limit0Demo(Name) values('Bob');
Query OK, 1 row affected (0.12 sec)
mysql> insert into Limit0Demo(Name) values('John');
Query OK, 1 row affected (0.11 sec)
mysql> insert into Limit0Demo(Name) values('Sam');
Query OK, 1 row affected (0.11 sec)
mysql> insert into Limit0Demo(Name) values('James');
Query OK, 1 row affected (0.12 sec)
mysql> insert into Limit0Demo(Name) values('Maxwell');
Query OK, 1 row affected (0.13 sec)
mysql> insert into Limit0Demo(Name) values('Ricky');
Query OK, 1 row affected (0.09 sec)
mysql> insert into Limit0Demo(Name) values('Adam');
Query OK, 1 row affected (0.06 sec)

Now you can display all records from the table using a select statement. The query is as follows −

mysql> select *from Limit0Demo;

The following is the output −

+----+---------+
| Id | Name    |
+----+---------+
|  1 | David   |
|  2 | Larry   |
|  3 | Carol   |
|  4 | Bob     |
|  5 | John    |
|  6 | Sam     |
|  7 | James   |
|  8 | Maxwell |
|  9 | Ricky   |
| 10 | Adam    |
+----+---------+
10 rows in set (0.00 sec)

Case 1: Use of limit 0.

The query is as follows −

mysql> select *from Limit0Demo limit 0;
Empty set (0.00 sec)

Case 2: When you want the specific number of rows from the table. The query is as follows −

mysql> select *from Limit0Demo limit 3;

The following is the output −

+----+-------+
| Id | Name  |
+----+-------+
|  1 | David |
|  2 | Larry |
|  3 | Carol |
+----+-------+
3 rows in set (0.00 sec)
raja
Published on 26-Feb-2019 16:56:33
Advertisements