How can we check for NULL in a MySQL query?

MySQLMySQLi Database

With the help of IS NULL operator, we can check for NULL in a MySQL query. We cannot use = (comparison operator) because as we know that NULL is not a value. Following example using the data from ‘employee’ table will exhibit it −

Example

mysql> Select * from Employee WHERE Salary IS NULL;
+----+-------+--------+
| ID | Name  | Salary |
+----+-------+--------+
| 7  | Aryan | NULL   |
| 8  | Vinay | NULL   |
+----+-------+--------+
2 rows in set (0.00 sec)

The query above use IS NULL operator and produces the output where salary column is having NULL.

mysql> Select * from employee where salary = NULL;
Empty set (0.00 sec)

The query above use = (Comparison operator) hence produces the empty set because with NULL is not a value.

raja
Published on 20-Feb-2018 14:21:38
Advertisements