How do I select data that does not have a null record in MySQL?


To select not-null records, use IS NOT NULL property. Let us first create a table −

mysql> create table DemoTable1792
     (
     Name varchar(20)
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1792 values('John Smith');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1792 values(NULL);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1792 values('David Miller');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1792 values(NULL);
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1792;

This will produce the following output −

+--------------+
| Name         |
+--------------+
| John Smith   |
| NULL         |
| David Miller |
| NULL         |
+--------------+
4 rows in set (0.00 sec)

Following is the query to select data that does not have a null record −

mysql> select * from DemoTable1792 where Name IS NOT NULL;

This will produce the following output −

+--------------+
| Name         |
+--------------+
| John Smith   |
| David Miller |
+--------------+
2 rows in set (0.00 sec)

Updated on: 23-Dec-2019

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements