SQL fundamentalseasy0-2 years
Why does `WHERE status <> 'CLOSED'` silently drop rows where status is NULL, and why can COUNT(*) and COUNT(column) disagree on the same table?
SQL's NULL means "unknown", not "empty", and every comparison against it — =, <>, <, even NULL = NULL — evaluates to unknown rather than true or false. WHERE keeps only rows where the condition is true, so a row with status IS NULL is neither included by status = 'CLOSED' nor by status <> 'CLOSED': both are unknown, both get filtered out. COUNT(*) counts rows; COUNT(column) counts non-NULL values of that column, so a table with rows where the column is NULL always has COUNT(column) <= COUNT(*), and reporting code that assumes they match undercounts silently.