The NOT IN clause is omitting the NULL values when checking the condition.
INPUT
ID NAME
1 A
2 <null>
3 C
SELECT... FROM...
WHERE NAME NOT IN ('C')
is only returning ID value 1. I need both 1 & 2.
Could this be done?
The NOT IN clause is omitting the NULL values when checking the condition.
INPUT
ID NAME
1 A
2 <null>
3 C
SELECT... FROM...
WHERE NAME NOT IN ('C')
is only returning ID value 1. I need both 1 & 2.
Could this be done?
Either you check NULL values
select *
from not_in
where name not in ('C') or name is null;
or you can convert NULL values in any other character with coalesce. I use ' ' in the sample below.
select *
from not_in
where coalesce(name, ' ') not in ('C');
Correct SQL would be
SELECT... FROM...
WHERE NAME NOT IN ('C')
or NAME is NULL
That's because any comparisons with NULL
also yield NULL
(i.e. not truthy nor falsy). (Credit to @Jack)
Refer:
MySQL: Why is NULL ignored in MySQL?
Working of Null Values
SELECT...
FROM...
WHERE NAME NOT IN ('C')
OR NAME IS NULL
SELECT...
FROM...
WHERE ISNULL(NAME, '') NOT IN ('C')