How should I compare NULL values?
Understand the concept
SQL NULL represents missing or unknown information, not an ordinary value such as zero or an empty string. Comparisons involving NULL usually produce unknown rather than true or false, and WHERE keeps only true conditions. Special predicates such as IS NULL make the intended check explicit.
Important ideas
-
Use
IS NULLorIS NOT NULL, not= NULL. -
NULL means unknown; ordinary comparisons with NULL are not true.
-
Be careful with
NOT INif the subquery can return NULL;NOT EXISTSis often clearer. -
COUNT(*)counts rows, whileCOUNT(column)skips NULL values in that column.
Can you explain it without the notes?
How should I compare NULL values?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
SQL NULL represents missing or unknown information, not an ordinary value such as zero or an empty string. Comparisons involving NULL usually produce unknown rather than true or false, and WHERE keeps only true conditions. Special predicates such as IS NULL make the intended check explicit.
Turn this concept into a clear answer.
- Mention three-valued logic and show why
= NULLdoes not find missing values.
Adapted from 05_SQL_POSTGRESQL.md · “How should I compare NULL values?”. Examples are study fragments; framework behavior can depend on your version.