Find the count of EMPTY or NULL columns in a MySQL table?


Let us first create a table −

mysql> create table DemoTable781 (
   Name varchar(100)
);
Query OK, 0 rows affected (0.66 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable781 values('');
Query OK, 1 row affected (0.29 sec)
mysql> insert into DemoTable781 values('Chris');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable781 values('');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable781 values(null);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable781 values(null);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable781 values('');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable781 values(null);
Query OK, 1 row affected (0.28 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable781;

This will produce the following output -

+-------+
| Name  |
+-------+
|       |
| Chris |
|       |
| NULL  |
| NULL  |
|       |
| NULL  |
+-------+
7 rows in set (0.00 sec)

Here is the query to find the count of EMPTY or NULL columns in a MySQL table −

mysql> (select SUM(CASE when Name IS NULL THEN 1 ELSE 0 END) AS NullCountAndEmptyCount from DemoTable781)
UNION ALL
(select SUM(CASE when Name='' THEN 1 ELSE 0 END) AS NullCountAndEmptyCount from DemoTable781);

This will produce the following output -

+------------------------+
| NullCountAndEmptyCount |
+------------------------+
| 3                      |
| 3                      |
+------------------------+
2 rows in set (0.00 sec)
raja
Published on 09-Sep-2019 11:40:53
Advertisements