Why does my SQL query treat NULL = NULL as neither true nor false?
Beginner question from a data analyst. Last week a report of mine undercounted customers by about four per cent. The bug was a filter like WHERE region <> 'KL', which I assumed would return everyone outside Kuala Lumpur. It also silently dropped every customer whose region was missing.
A colleague shrugged and said, "SQL uses three-valued logic." Apparently NULL = NULL doesn't come out true. It comes out as a third thing.
So, three questions:
- Is this the same "many-valued logic" philosophers talk about, or just a database quirk with a fancy name?
- If it is a real logic, which one?
- What is NULL supposed to mean? "We don't know the region" and "this customer has no region" seem like very different situations, but the database treats them the same.