What are the NULL pitfalls in SQL, and why can NOT IN return nothing?
Short answer
SQL uses three-valued logic: comparing with NULL yields UNKNOWN, and WHERE keeps only TRUE. So x NOT IN (1, 2, NULL) is never TRUE, and AVG, SUM, COUNT(col) ignore NULL, which easily skews numbers.
3 points to say:
- Comparing with NULL gives UNKNOWN;
WHEREkeeps only TRUE, so test for NULL withIS NULL. - Use
NOT EXISTS, neverNOT INon a column that can be NULL. AVG,SUM,COUNT(col)ignore NULL; the best defense isNOT NULLon every column with no reason to be empty.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.