
- 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
How to count null values in MySQL?
To count null values in MySQL, you can use CASE statement. Let us first see an example and create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20) ); Query OK, 0 rows affected (0.77 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values(null); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(FirstName) values(''); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(FirstName) values('Larry'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(FirstName) values(''); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName) values(null); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(FirstName) values(null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName) values('Bob'); Query OK, 1 row affected (0.15 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | NULL | | 3 | | | 4 | Larry | | 5 | | | 6 | NULL | | 7 | NULL | | 8 | Bob | +----+-----------+ 8 rows in set (0.00 sec)
Here is the query to count Null values in MySQL −
mysql> select sum(case when FirstName IS NULL then 1 else 0 end) as NUMBER_OF_NULL_VALUE from DemoTable;
This will produce the following output −
+----------------------+ | NUMBER_OF_NULL_VALUE | +----------------------+ | 3 | +----------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to use a single MySQL query to count column values ignoring null?
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- How to skip blank and null values in MySQL?
- Count NOT NULL values from separate tables in a single MySQL query
- How to count distinct values in MySQL?
- Count zero, NULL and distinct values except zero and NULL with a single MySQL query
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records
- How to concat Values in MySQL Query and to handle Null values as well?
- Count only null values in two different columns and display in one MySQL select statement?
- MySQL query to order by NULL values
- Looping in JavaScript to count non-null and non-empty values
- MySQL query to convert empty values to NULL?
- Display 1 for NULL values in MySQL
- How to return distinct values in MySQL and their count?
- MySQL query to set values for NULL occurrence

Advertisements