SQLPathLab
Practical SQL Guide · MySQL 8.0

Why NULL and three-valued logic cause errors

NULL is not an empty string, and comparisons with NULL can be UNKNOWN.

Problem and reasoning

SQL predicates can produce TRUE, FALSE, or UNKNOWN. NULL represents missing or unknown information, so status = NULL does not find missing values, and NOT does not automatically turn UNKNOWN into TRUE.

Use IS NULL or IS NOT NULL for filtering and COALESCE for display fallbacks. COUNT(*), COUNT(column), SUM, and AVG also treat NULL differently, so define the intended denominator and missing-value meaning before aggregating.

Do not replace every NULL with zero or an empty string. An unknown amount, a zero amount, and an unfilled text field often represent different business states.

Examples

Sample table

Table:customers
customers
idnickname
101 NULL
102 Bobby
103 NULL

Before / Not recommended

SELECT id, nickname FROM customers WHERE nickname = NULL ORDER BY id;

Before result

result
idnickname

After / Recommended

SELECT id, nickname FROM customers WHERE nickname IS NULL ORDER BY id;

After result

result
idnickname
101 NULL
103 NULL

Why it is better:Equality with NULL never becomes TRUE; IS NULL returns the customers whose nickname is missing.

Was this explanation clear?