Introduction
NULL means unknown in databases. Understanding how NULL works with AND, OR, and NOT helps you get correct answers when data is missing or unknown.
condition1 AND condition2 condition1 OR condition2 NOT condition
TRUE AND NULL
FALSE OR NULL
NOT NULL
TRUE OR NULL
SELECT TRUE AND NULL AS and_result, FALSE OR NULL AS or_result, NOT NULL AS not_result, TRUE OR NULL AS or_true_result;
TRUE AND NULL?AND returns FALSE if any side is FALSE. If one side is TRUE and the other is NULL, the result is NULL because the unknown value affects the outcome.TRUE AND NULLTRUE and the second is NULL, the result is NULL.col is NOT NULL?= or != to compare with NULL. Instead, use IS NULL or IS NOT NULL.col is not null is col IS NOT NULL.Test with a column flag that can be TRUE, FALSE, or NULL, what is the result of this query?SELECT * FROM Test WHERE flag OR FALSE;
OR FALSE returns the value of the first operand because FALSE does not affect the OR result.flag OR FALSEflag is TRUE, the expression is TRUE. If flag is FALSE, expression is FALSE. If flag is NULL, expression is NULL (unknown), which is treated as FALSE in WHERE filtering.WHERE NOT (col = NULL)
col = NULL always returns NULL (unknown), never TRUE or FALSE.NOT NULL is still NULL, so the condition never evaluates to TRUE, filtering out all rows.Employees where the ManagerID is either NULL or the employee is active (Active = TRUE). Which WHERE clause correctly handles NULL logic?IS NULL to check if ManagerID is NULL. Using = NULL is invalid and returns NULL.ManagerID is NULL or Active is TRUE, so use OR between these checks.Pass the test and this pattern moves unknown