Why we cannot use comparison operator(=) for getting the rows with NULL from a table?


We cannot use = (comparison operator) because we know that NULL is not a value. If we want to get the rows with NULL from a table then we need to use IS NULL operator in MySQL query. 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: 06-Feb-2020

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements