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;

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;

This will produce the following output −

+-----------+
| FirstName |
+-----------+
| John      |
| Chris     |
| Robert    |
+-----------+
3 rows in set (0.02 sec)
raja
Published on 11-Jul-2019 15:41:36
Advertisements