Display records ignoring NULL in MySQL


Use IS NOT NULL to display only NOT NULL records. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> FirstName varchar(100)
   -> );
Query OK, 0 rows affected (3.01 sec)

Insert some records in the table using insert command −

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

mysql> insert into DemoTable values(NULL);
Query OK, 1 row affected (0.58 sec)

mysql> insert into DemoTable values('Chris');
Query OK, 1 row affected (0.31 sec)

mysql> insert into DemoTable values(NULL);
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.34 sec)

mysql> insert into DemoTable values(NULL);
Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+-----------+
| FirstName |
+-----------+
| John      |
| NULL      |
| Chris     |
| NULL      |
| Robert    |
| NULL      |
+-----------+
6 rows in set (0.00 sec)

Following is the query to display records ignoring NULL −

mysql> select *from DemoTable where FirstName IS NOT NULL;

Output

This will produce the following output −

+-----------+
| FirstName |
+-----------+
| John      |
| Chris     |
| Robert    |
+-----------+
3 rows in set (0.02 sec)

Updated on: 30-Jun-2020

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements