
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Why is “LIMIT 0” even allowed in MySQL SELECT statements?
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)
- Related Articles
- SELECT last entry without using LIMIT in MySQL?
- MySQL LIMIT to select a single row
- Limit length of longtext field in MySQL SELECT results?
- Implement case sensitivity in MySQL SELECT statements
- How to select the top two values using LIMIT in MySQL?
- How to select first and last row record using LIMIT in MySQL?
- Select different fields in MySQL even if a field is set to null?
- 0 is even prime number
- MySQL query to get result from multiple select statements?
- Can we use a comma between MySQL SELECT statements?
- Can we select second largest record from a table without using LIMIT clause in MySQL?
- MySQL CASE WHEN with SELECT to display odd and even ids?
- How to limit the number of characters allowed in form input text field?
- Why we mention in MySQL WHERE 1=0?
- What is the limit of auto_increment (integer) in MySQL?
