Why it happens: WHERE with <> also excludes NULLs
WITH customers(id, name, city) AS (
VALUES
(1, 'Amy', 'London'),
(2, 'Liam', 'Manchester'),
(3, 'Megan', NULL),
(4, 'Peter', 'Leeds'),
(5, 'Sara', NULL)
)
SELECT id, name, city
FROM customers
WHERE city <> 'London'
ORDER BY id;| id | name | city |
|---|---|---|
| 2 | Liam | Manchester |
| 4 | Peter | Leeds |
Megan and Sara do not appear because their city is unknown. SQL cannot say that an unknown city is different from London, so the condition never becomes TRUE.
How to fix it in SQL
WITH customers(id, name, city) AS (
VALUES
(1, 'Amy', 'London'),
(2, 'Liam', 'Manchester'),
(3, 'Megan', NULL),
(4, 'Peter', 'Leeds'),
(5, 'Sara', NULL)
)
SELECT id, name, city
FROM customers
WHERE city <> 'London'
OR city IS NULL
ORDER BY id;| id | name | city |
|---|---|---|
| 2 | Liam | Manchester |
| 3 | Megan | NULL |
| 4 | Peter | Leeds |
| 5 | Sara | NULL |
This form is portable and makes the policy visible: «city other than London or unknown city». Do not use COALESCE(city, '...') with a made-up value unless that value has a real meaning and cannot collide with valid data.
How to check it
In the corrected example, Manchester, Leeds and the two customers with no city should appear.
How to avoid it next time
NOT does not turn an unknown condition into a true one.
Related cases worth checking
The important question is not «how do I force NULL in?» but «what does my rule mean?»
Before fixing the SQL, define the intent. There are at least two valid rules, and they produce different results:
| Intent | How NULL is treated | Portable predicate |
|---|---|---|
| I want only known cities other than London | NULL is left out | city IS NOT NULL AND city <> 'London' |
| I want every row except those confirmed as London | NULL is included | city <> 'London' OR city IS NULL |
The first rule makes it explicit that an unknown city is not enough to classify the customer as «outside London». The second expresses a different policy: it excludes only the rows we know are London and keeps those that have no city yet.
Null-safe predicates: useful, but the syntax differs between engines
Some engines offer comparisons that always return true or false, even when they involve NULL. PostgreSQL and SQLite support IS DISTINCT FROM; SQL Server supports it in recent versions, and MySQL uses the null-safe operator <=> for equality. If your goal is portability, <> ... OR ... IS NULL is usually the more explicit choice across engines.
| Engine | Relevant rule | Null-safe alternative |
|---|---|---|
| PostgreSQL | Ordinary comparisons with NULL give an unknown result. | city IS DISTINCT FROM 'London' |
| MySQL 8.4 | =, <> and other ordinary comparisons with NULL return NULL. | NOT (city <=> 'London') |
| SQL Server | With the current ANSI behaviour, a comparison with NULL gives UNKNOWN. | IS DISTINCT FROM in versions that support it; if you want portability, use the explicit condition. |
| SQLite | Ordinary operators usually return NULL when an operand is NULL. | IS DISTINCT FROM; it also has its own semantics for IS/IS NOT. |