
- 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
Check if a table is empty or not in MySQL using EXISTS
The following is the syntax to check whether a table is empty or not using MySQL EXISTS −
SELECT EXISTS(SELECT 1 FROM yourTableName);
Example
First, let us create a table. The query to create a table is as follows −
mysql> create table ReturnDemo -> ( -> Id int, -> Name varchar(10) -> ); Query OK, 0 rows affected (0.79 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into ReturnDemo values(100,'Larry'); Query OK, 1 row affected (0.18 sec) mysql> insert into ReturnDemo values(101,'Bob'); Query OK, 1 row affected (0.28 sec) mysql> insert into ReturnDemo values(102,'Sam'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from ReturnDemo;
Output
+------+-------+ | Id | Name | +------+-------+ | 100 | Larry | | 101 | Bob | | 102 | Sam | +------+-------+ 3 rows in set (0.00 sec)
Here is the query to check if MySQL table is empty or not −
mysql> select exists(select 1 from ReturnDemo) AS Output;
Output
+--------+ | Output | +--------+ | 1 | +--------+ 1 row in set (0.00 sec)
The output 1 tells that the MySQL table isn’t empty.
- Related Articles
- MySQL query to check if database is empty or not?
- How to check if a file exists or not using Python?
- How can you test if some record exists or not in a MySQL table using Python?
- Check if table exists in MySQL and display the warning if it exists?
- How to check if a table exists in MySQL and create if it does not already exist?
- Java Program to check if a string is empty or not
- Get boolean result whether table exists or not using CASE WHEN in MySQL
- Check if a value exists in a column in a MySQL table?
- Check that a table exists in MySQL?
- How to check if a file exists or not in Java?
- Check if a word exists in a grid or not in Python
- How do you check if a ResultSet is empty or not in JDBC?
- How to check if a text field is empty or not in swift?
- How to check if the Azure resource group is empty or not using PowerShell?
- How to check if field is null or empty in MySQL?

Advertisements