MySQL query to remove Null Records in a column?


To remove NULL records in a column, you can use delete command. Following is the syntax −

delete from yourTableName where yourColumnName IS NULL;

Let us first create a table −

mysql> create table removeNullRecordsDemo
   -> (
   -> Name varchar(100)
   -> );
Query OK, 0 rows affected (0.50 sec)

Following is the query to insert records in the table using insert command −

mysql> insert into removeNullRecordsDemo values('John');
Query OK, 1 row affected (0.14 sec)

mysql> insert into removeNullRecordsDemo values(null);
Query OK, 1 row affected (0.15 sec)

mysql> insert into removeNullRecordsDemo values('Larry');
Query OK, 1 row affected (0.19 sec)

mysql> insert into removeNullRecordsDemo values('Bob');
Query OK, 1 row affected (0.12 sec)

mysql> insert into removeNullRecordsDemo values(null);
Query OK, 1 row affected (0.13 sec)

mysql> insert into removeNullRecordsDemo values('David');
Query OK, 1 row affected (0.18 sec)

mysql> insert into removeNullRecordsDemo values(null);
Query OK, 1 row affected (0.22 sec)

Following is the query to display all records from the table using select statement −

mysql> select *from removeNullRecordsDemo;

This will produce the following output −

+-------+
| Name  |
+-------+
| John  |
| NULL  |
| Larry |
| Bob   |
| NULL  |
| David |
| NULL  |
+-------+
7 rows in set (0.00 sec)

Let us now remove NULL records in the above column −

mysql> delete from removeNullRecordsDemo where Name IS NULL;
Query OK, 3 rows affected (0.16 sec)

Check the null records have been removed from column or not −

mysql> select * from removeNullRecordsDemo;

Following is the output displaying all records except NULL −

+-------+
| Name  |
+-------+
| John  |
| Larry |
| Bob   |
| David |
+-------+
4 rows in set (0.00 sec)

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements