In MySQL what is the difference between != NULL and IS NOT NULL?

MySQLMySQLi Database

If you compare a value with !=NULL then it returns NULL. So, the !=NULL is meaningless. To see the difference between !=NULL and IS NOT NULL, let us first create a table.

Let us first create a table −

mysql> create table DemoTable1970
   (
   Value int
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1970 values(10);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1970 values(NULL);
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1970;

This will produce the following output −

+-------+
| Value |
+-------+
|    10 |
|  NULL |
+-------+
2 rows in set (0.00 sec)

Here is the query that will let you understand the difference −

mysql> select Value!=NULL as Output1,Value IS NOT NULL as Output2 from DemoTable1970;

This will produce the following output −

+---------+---------+
| Output1 | Output2 |
+---------+---------+
|    NULL |       1 |
|    NULL |       0 |
+---------+---------+
2 rows in set (0.00 sec)
raja
Published on 31-Dec-2019 12:15:54
Advertisements