Full answer
Join conditions are ordinary comparisons, so NULL follows the same three-valued logic as in WHERE.
WITH a(k, v) AS (VALUES (1, 'a1'), (NULL, 'a-null')),
b(k, w) AS (VALUES (1, 'b1'), (NULL, 'b-null'))
SELECT a.v, b.w FROM a JOIN b ON a.k = b.k ORDER BY a.v;| v | w |
|---|---|
| a1 | b1 |
Only the key 1 rows matched. The NULL on the left did not match the NULL on the right.
A LEFT JOIN keeps the unmatched left rows, including the one with a NULL key:
WITH a(k, v) AS (VALUES (1, 'a1'), (NULL, 'a-null')),
b(k, w) AS (VALUES (1, 'b1'), (NULL, 'b-null'))
SELECT a.v, b.w FROM a LEFT JOIN b ON a.k = b.k ORDER BY a.v;| v | w |
|---|---|
| a-null | NULL |
| a1 | b1 |
The orders table in the shop schema shows the business version. Guest order 106 has no customer, so it disappears from an inner join with customers:
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);SELECT COUNT(*) AS orders_in_inner_join,
(SELECT COUNT(*) FROM orders) AS all_orders
FROM orders o
JOIN customers c ON c.id = o.customer_id;| orders_in_inner_join | all_orders |
|---|---|
| 5 | 6 |
Five of six orders appear. A revenue report built on that inner join would quietly leave out guest orders. Starting from orders with a LEFT JOIN keeps them.
When NULL keys should match (for example, comparing two snapshots where "no region" in both counts as the same), use ON a.k IS NOT DISTINCT FROM b.k. Expect it to be slower on large tables, since it may not use an index the way = does.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
WITH a(k, v) AS (VALUES (1, 'a1'), (NULL, 'a-null')),
b(k, w) AS (VALUES (1, 'b1'), (NULL, 'b-null'))
SELECT a.v, b.w FROM a JOIN b ON a.k = b.k ORDER BY a.v;
WITH a(k, v) AS (VALUES (1, 'a1'), (NULL, 'a-null')),
b(k, w) AS (VALUES (1, 'b1'), (NULL, 'b-null'))
SELECT a.v, b.w FROM a LEFT JOIN b ON a.k = b.k ORDER BY a.v;
SELECT COUNT(*) AS orders_in_inner_join,
(SELECT COUNT(*) FROM orders) AS all_orders
FROM orders o
JOIN customers c ON c.id = o.customer_id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- NULL keys never match with =
- Inner joins drop rows with NULL keys
- LEFT JOIN keeps them with NULLs on the other side
- IS NOT DISTINCT FROM makes NULLs match when that is truly wanted
Common mistakes
- Losing guest or unassigned rows in an inner join report
- Expecting NULL keys on both sides to join
Follow-ups
- How would you count orders with no customer?
- Should foreign key columns allow NULL?
Tips
- Tie it back to a report that silently loses rows