In SQL, comparing between a null value and any other value (including another null ) using a comparison operator (e.g. = != , < , Etc.) will result in null , which is considered false for the purpose of the where clause (strictly speaking, it is not true, not false, but the effect is the same).
The reason is that null means โunknownโ, so the result of any comparison with null also โunknownโ. This way, you wonโt get hit on the lines if you where my_column = null .
SQL provides special syntax for checking if the column is null through is null and is not null , which is a special condition for checking for null (or not null ).
Here are some SQL showing the various conditions and their effects, as described above.
create table t (x int, y int); insert into t values (null, null), (null, 1), (1, 1); select 'x = null' as test , x, y from t where x = null union all select 'x != null', x, y from t where x != null union all select 'not (x = null)', x, y from t where not (x = null) union all select 'x = y', x, y from t where x = y union all select 'not (x = y)', x, y from t where not (x = y);
returns only 1 row (as expected):
TEST XY x = y 1 1
See how SQLFiddle works
Bohemian Mar 06 2018-12-12T00: 00Z
source share