Why is NULL more dangerous than 0?
How SQL's "unknown" value quietly hides broken data that a `0` never could.
- three-valued logic
- NOT NULL DEFAULT 0
0 is a known, concrete value — it means "zero, exactly." NULL means "unknown / not applicable / missing" — it's the absence of a value. SQL treats them completely differently:
| Behavior | 0 |
NULL |
|---|---|---|
| Arithmetic | 5 + 0 = 5 |
5 + NULL = NULL (poisons the whole expression) |
| Equality | 0 = 0 → TRUE |
NULL = NULL → UNKNOWN (not TRUE!) |
| Filtering | WHERE amount = 0 works as expected |
WHERE amount = NULL never matches anything — you must use IS NULL |
| Aggregates | Counted in AVG, COUNT, etc. |
Silently excluded from AVG/COUNT, changing the result set size |
| Boolean logic | Two-valued (TRUE/FALSE) | Three-valued (TRUE/FALSE/UNKNOWN) — UNKNOWN rows get dropped from WHERE/JOIN silently, no error |
The danger isn't that NULL is "wrong" — it's that it fails silently. A 0 gives you a wrong-but-visible number. A NULL can make rows vanish from a query with no warning, which is far worse in a system where money is involved.
Fintech example
Imagine an accounts table used by a nightly job that flags overdrawn accounts:
SELECT account_id
FROM accounts
WHERE balance <= 0;
- Account A:
balance = 0→ matches, correctly flagged as "empty, watch it." - Account B:
balance = NULL(balance was never computed due to a sync bug) →NULL <= 0evaluates toUNKNOWN, so this row is silently skipped.
Account B — the one with an actual data problem and possibly a real negative balance underneath — never gets flagged for review. Nobody gets an error. The report just looks "clean." In a banking/fintech context that's exactly how broken accounts, unprocessed refunds, or missing interest-rate fields slip through compliance and reconciliation checks undetected — versus a 0, which at least shows up and gets scrutinized.
The fix is usually to be explicit: WHERE balance <= 0 OR balance IS NULL, or better, disallow NULL on financial columns via NOT NULL DEFAULT 0 when "unknown" isn't a valid business state.
Description: A conceptual database/SQL question about the semantic and behavioral difference between NULL (unknown/missing) and 0 (a known zero value), focusing on how SQL's three-valued logic causes NULLs to silently disappear from filters, joins, and aggregates — a subtle but high-impact data-integrity risk in financial systems.
No comments yet.
Sign in to leave a comment.