I have a table with the following data:
id | value
1 | 1
2 | NULL
3 | NULL
My query:
SELECT * FROM table WHERE value != '1'
Result is empty.
Why does the query do not return row 2
and 3
?
I have a table with the following data:
id | value
1 | 1
2 | NULL
3 | NULL
My query:
SELECT * FROM table WHERE value != '1'
Result is empty.
Why does the query do not return row 2
and 3
?
When value
is equal to NULL
the predicate becomes: NULL != '1'
. This evaluates to NULL
, not true
, as one might expect.
This is the so-called three valued logic of SQL: an expression evaluates to either true, or false, or NULL
.
To correctly check against NULL
you need to use IS NULL
:
WHERE (value != '1') OR (value IS NULL)