How should I compare NULL values?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn How should I compare NULL values?
Definition:
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.
Key points:
-
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.
Interview point:
- Mention three-valued logic and show why
= NULLdoes not find missing values.
Adapted from 05_SQL_POSTGRESQL.md · “How should I compare NULL values?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.