SQL error · Missing rows

WHERE with <> also excludes NULLs

Problem: An ordinary comparison with NULL gives unknown, and WHERE discards it.

Solution: If missing values should be included, add OR column IS NULL to the condition.

Why it happens: WHERE with <> also excludes NULLs

Problem query
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;
Result verified locally in SQLite
idnamecity
2LiamManchester
4PeterLeeds

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.

What the problem looks like

No error appears: WHERE city <> 'London' does not return the rows where city is NULL.

Since the engine gives no warning, count the rows where the column is NULL and decide whether they should appear in the result.

How to fix it in SQL

Exclude only confirmed London
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;
Verified result
idnamecity
2LiamManchester
3MeganNULL
4PeterLeeds
5SaraNULL

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:

IntentHow NULL is treatedPortable predicate
I want only known cities other than LondonNULL is left outcity IS NOT NULL AND city <> 'London'
I want every row except those confirmed as LondonNULL is includedcity <> '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.

EngineRelevant ruleNull-safe alternative
PostgreSQLOrdinary 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 ServerWith 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.
SQLiteOrdinary operators usually return NULL when an operand is NULL.IS DISTINCT FROM; it also has its own semantics for IS/IS NOT.