Difference Between MySql <> NULL and IS NOT NULL?


If you compare the <> operator with NULL value then you will get NULL value always and no result.

Let us see some examples for comparison −

mysql> select 10 <> NULL;
+------------+
| 10 <> NULL |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)
mysql> select NULL <> NULL;
+--------------+
| NULL <> NULL |
+--------------+
| NULL         |
+--------------+
1 row in set (0.00 sec)
mysql> select 'Chris' <> NULL;
+-----------------+
| 'Chris' <> NULL |
+-----------------+
| NULL            |
+-----------------+
1 row in set (0.00 sec)
mysql> select '' <> NULL;
+------------+
| '' <> NULL |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable846 values(NULL);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable846 values(10);
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable846;

This will produce the following output −

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

Following is the result for IS NOT NULL operator −

mysql> select *from DemoTable846 where Value IS NOT NULL;

This will produce the following output −

+-------+
| Value |
+-------+
| 10    |
+-------+
1 row in set (0.00 sec)

Here is the result for <> operator. Following is the query −

mysql> select *from DemoTable846 where Value <> NULL;
Empty set (0.00 sec)

Updated on: 03-Jul-2020

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements