Why can WHERE x NOT IN (SELECT y FROM t) return no rows when the subquery yields a NULL?
Correct answer
B. Because x <> NULL is UNKNOWN, so the NOT IN predicate can never be TRUE; NOT EXISTS is null-safe
17 views
B. Because x <> NULL is UNKNOWN, so the NOT IN predicate can never be TRUE; NOT EXISTS is null-safe
17 views
NOT IN and NULL: the predicate expands to a chain of ANDed comparisons, and one UNKNOWN makes the whole expression UNKNOWN, which filters the row out. A single NULL in the subquery can therefore silently empty the result, a classic production bug. NOT EXISTS with a correlated condition, or filtering the subquery with WHERE y IS NOT NULL, restores the intended semantics.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is B? Ask here and get an explanation.