How can we check for NULL in a MySQL query?


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.

Updated on: 20-Jun-2020

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements