I am confused why here the result is TRUE
select 'Clark' in ('Dave', null, 'Clark');
But here it’s NULL (instead of evaluating to TRUE)
select 'Ava' not in ('Dave', null, 'Clark');
Can somebody please explain?
I am confused why here the result is TRUE
select 'Clark' in ('Dave', null, 'Clark');
But here it’s NULL (instead of evaluating to TRUE)
select 'Ava' not in ('Dave', null, 'Clark');
Can somebody please explain?
2
Answers
SELECT 'Clark' IN ('Dave', NULL, 'Clark');
→ TRUE‘Clark’ is in the list, so IN returns TRUE, ignoring NULL.
SELECT 'Ava' NOT IN ('Dave', NULL, 'Clark');
→ NULLNOT IN
checks all values. SinceNULL
makesAva <> NULL
unknown, the result isNULL
.To fix it,You should use
IS DISTINCT FROM
to avoidNULL
issues: here is an example:This returns
TRUE
In
SQL
, logical operations are three-valued because of the presence ofNULL
(missing data). Clark = NULL cannot be defined asTRUE
orFLASE
because it is not known what it is compared to.In the first example, you check whether Clark is present in the list Dave, null and Clark:
Clearly Clark is present in the list, despite the unknown (NULL) value
In the second example, you check whether Ava is not present (it really doesn’t matter) in the list Dave, null, Clark:
The problem here is, we don’t know what Ava = NULL actually returns. Depending on the data, it can be either
TRUE
orFLASE
. Since there is no way to determine this unambiguously, the final result is NULL (cannot be determined)