SQL Outer Joins: LEFT, RIGHT and FULL JOIN
How outer joins keep rows that have no match: LEFT, RIGHT and FULL JOIN, the NULLs they produce, finding rows without a partner, why a WHERE condition can silently turn an outer join into an inner join, chains of outer joins, and Oracle's old (+) syntax.
kvi5hnuCode Sprout team member10 min readOn this page
An inner join (article 12) only keeps rows that have a partner on the other side. That's often wrong for reports. "Sales per customer" should list customers who bought nothing, with zero sales. "Stock and sales per product" should include products that never sold. An outer join keeps the rows without a partner, and fills the missing side's columns with NULL.
There are three kinds:
LEFT [OUTER] JOINkeeps every row of the left table, the one written beforeJOIN.RIGHT [OUTER] JOINkeeps every row of the right table.FULL [OUTER] JOINkeeps every row of both tables.
The word OUTER is optional and usually left out. The examples use the shop database from article 12.
LEFT JOIN
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
category VARCHAR(20) NOT NULL,
brand VARCHAR(20),
price DECIMAL(8, 2) NOT NULL,
stock INTEGER NOT NULL,
weight_g INTEGER
);
INSERT INTO products VALUES
(1, 'Ridge 2 tent', 'tents', 'Alpina', 249.99, 12, 2100),
(2, 'Summit 4 tent', 'tents', 'Alpina', 489.00, 3, 4300),
(3, 'Glow head torch', 'lighting', 'Lumen', 34.50, 40, 90),
(4, 'Basecamp lantern', 'lighting', NULL, 27.00, 15, NULL),
(5, 'Trail 30 backpack', 'packs', 'Kestrel', 89.95, 22, 1150),
(6, 'Day 18 backpack', 'packs', 'Kestrel', 54.00, 0, 640),
(7, 'Merino socks', 'clothing', 'Woolly', 16.00, 75, 80);
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
city VARCHAR(30),
referred_by INTEGER REFERENCES customers (customer_id)
);
INSERT INTO customers VALUES
(1, 'Asha Rao', 'Pune', NULL),
(2, 'Ben Okafor', 'Leeds', 1),
(3, 'Chen Wei', 'Pune', 1),
(4, 'Dana Cruz', 'York', 2),
(5, 'Eli Novak', 'Leeds', NULL),
(6, 'Farah Haddad', NULL, 3);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers (customer_id), -- NULL = guest checkout
placed_on DATE NOT NULL,
status VARCHAR(10) NOT NULL
);
INSERT INTO orders VALUES
(101, 1, DATE '2026-03-02', 'shipped'),
(102, 2, DATE '2026-03-05', 'shipped'),
(103, 1, DATE '2026-03-09', 'delivered'),
(104, 3, DATE '2026-03-15', 'pending'),
(105, 4, DATE '2026-03-20', 'cancelled'),
(106, NULL, DATE '2026-03-22', 'shipped');
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders (order_id),
product_id INTEGER REFERENCES products (product_id),
qty INTEGER NOT NULL,
unit_price DECIMAL(8, 2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);
INSERT INTO order_items VALUES
(101, 1, 1, 249.99), (101, 7, 3, 16.00),
(102, 3, 2, 34.50),
(103, 5, 1, 89.95), (103, 3, 1, 34.50), (103, 7, 2, 15.00),
(104, 2, 1, 489.00),
(105, 6, 1, 54.00),
(106, 7, 1, 16.00);
\pset null '(null)'
-- Every customer, with their orders if they have any
SELECT c.customer_id, c.name, o.order_id, o.status
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
ORDER BY c.customer_id, o.order_id;
-- Only the customers with no orders: keep the rows where the right side is missing
SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL
ORDER BY c.customer_id;
customer_id | name | order_id | status
-------------+--------------+----------+-----------
1 | Asha Rao | 101 | shipped
1 | Asha Rao | 103 | delivered
2 | Ben Okafor | 102 | shipped
3 | Chen Wei | 104 | pending
4 | Dana Cruz | 105 | cancelled
5 | Eli Novak | (null) | (null)
6 | Farah Haddad | (null) | (null)
(7 rows)
customer_id | name
-------------+--------------
5 | Eli Novak
6 | Farah Haddad
(2 rows)
In the first query, every customer appears. The matched pairs come out exactly as in an inner join. Asha appears twice, once for each of the two orders placed by Asha. Eli and Farah have no orders, so each appears once, with NULL in every column from orders.
So the rule is: a LEFT JOIN produces all the rows the inner join would produce, plus one row for each left-table row that matched nothing, padded with NULLs.

Finding rows without a partner
The second query is a classic pattern: an outer join followed by WHERE right_side.key IS NULL keeps only the unmatched rows, here the customers who have never ordered. Test a column that can't be NULL in a real match, such as the right table's primary key. If you tested a nullable column, like o.status in a table where status could be NULL, you'd confuse "no match" with "a match whose column happens to be NULL".
This is called an anti-join. NOT EXISTS expresses the same thing more directly, and article 16 compares the options.
The WHERE-clause trap
This is the most common outer-join bug, and it's easy to write:
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
category VARCHAR(20) NOT NULL,
brand VARCHAR(20),
price DECIMAL(8, 2) NOT NULL,
stock INTEGER NOT NULL,
weight_g INTEGER
);
INSERT INTO products VALUES
(1, 'Ridge 2 tent', 'tents', 'Alpina', 249.99, 12, 2100),
(2, 'Summit 4 tent', 'tents', 'Alpina', 489.00, 3, 4300),
(3, 'Glow head torch', 'lighting', 'Lumen', 34.50, 40, 90),
(4, 'Basecamp lantern', 'lighting', NULL, 27.00, 15, NULL),
(5, 'Trail 30 backpack', 'packs', 'Kestrel', 89.95, 22, 1150),
(6, 'Day 18 backpack', 'packs', 'Kestrel', 54.00, 0, 640),
(7, 'Merino socks', 'clothing', 'Woolly', 16.00, 75, 80);
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
city VARCHAR(30),
referred_by INTEGER REFERENCES customers (customer_id)
);
INSERT INTO customers VALUES
(1, 'Asha Rao', 'Pune', NULL),
(2, 'Ben Okafor', 'Leeds', 1),
(3, 'Chen Wei', 'Pune', 1),
(4, 'Dana Cruz', 'York', 2),
(5, 'Eli Novak', 'Leeds', NULL),
(6, 'Farah Haddad', NULL, 3);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers (customer_id), -- NULL = guest checkout
placed_on DATE NOT NULL,
status VARCHAR(10) NOT NULL
);
INSERT INTO orders VALUES
(101, 1, DATE '2026-03-02', 'shipped'),
(102, 2, DATE '2026-03-05', 'shipped'),
(103, 1, DATE '2026-03-09', 'delivered'),
(104, 3, DATE '2026-03-15', 'pending'),
(105, 4, DATE '2026-03-20', 'cancelled'),
(106, NULL, DATE '2026-03-22', 'shipped');
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders (order_id),
product_id INTEGER REFERENCES products (product_id),
qty INTEGER NOT NULL,
unit_price DECIMAL(8, 2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);
INSERT INTO order_items VALUES
(101, 1, 1, 249.99), (101, 7, 3, 16.00),
(102, 3, 2, 34.50),
(103, 5, 1, 89.95), (103, 3, 1, 34.50), (103, 7, 2, 15.00),
(104, 2, 1, 489.00),
(105, 6, 1, 54.00),
(106, 7, 1, 16.00);
\pset null '(null)'
-- Trap: a WHERE condition on the right table removes the unmatched rows again
SELECT c.name, o.order_id, o.status
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.status = 'shipped'
ORDER BY c.name;
-- Fix: put the condition in ON, so it only decides which orders match
SELECT c.name, o.order_id, o.status
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
AND o.status = 'shipped'
ORDER BY c.name;
name | order_id | status
------------+----------+---------
Asha Rao | 101 | shipped
Ben Okafor | 102 | shipped
(2 rows)
name | order_id | status
--------------+----------+---------
Asha Rao | 101 | shipped
Ben Okafor | 102 | shipped
Chen Wei | (null) | (null)
Dana Cruz | (null) | (null)
Eli Novak | (null) | (null)
Farah Haddad | (null) | (null)
(6 rows)
The goal is "every customer, with their shipped orders, if any". The first query puts o.status = 'shipped' in WHERE, and the result has only two customers. Here's what happened:
- The
LEFT JOINproduced seven rows, including Eli and Farah with NULL order columns. WHEREthen testedo.status = 'shipped'on every row. For Eli and Farah,NULL = 'shipped'is unknown, so their rows were removed. Chen's and Dana's orders aren't shipped, so their rows were removed too.
A WHERE condition that rejects NULLs from the right table removes exactly the rows the outer join added, so the query behaves like an inner join. Optimizers even detect this and convert the join to an inner join.
The second query moves the condition into ON. Now it's part of the matching rule: a customer matches only their shipped orders. Customers with no shipped orders still appear once, with NULLs. Every customer appears, which was the goal.
The rule of thumb for a LEFT JOIN:
- conditions on the right table that decide which rows count as a match go in
ON; - conditions on the left table that decide which rows appear at all go in
WHERE.
A condition on the left table in ON is a different mistake: LEFT JOIN orders o ON o.customer_id = c.customer_id AND c.city = 'Pune' doesn't remove the customers from other cities. It only stops them from matching any orders, so they appear with NULLs. To keep only customers from Pune, write WHERE c.city = 'Pune'.
FULL JOIN and RIGHT JOIN
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
category VARCHAR(20) NOT NULL,
brand VARCHAR(20),
price DECIMAL(8, 2) NOT NULL,
stock INTEGER NOT NULL,
weight_g INTEGER
);
INSERT INTO products VALUES
(1, 'Ridge 2 tent', 'tents', 'Alpina', 249.99, 12, 2100),
(2, 'Summit 4 tent', 'tents', 'Alpina', 489.00, 3, 4300),
(3, 'Glow head torch', 'lighting', 'Lumen', 34.50, 40, 90),
(4, 'Basecamp lantern', 'lighting', NULL, 27.00, 15, NULL),
(5, 'Trail 30 backpack', 'packs', 'Kestrel', 89.95, 22, 1150),
(6, 'Day 18 backpack', 'packs', 'Kestrel', 54.00, 0, 640),
(7, 'Merino socks', 'clothing', 'Woolly', 16.00, 75, 80);
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
city VARCHAR(30),
referred_by INTEGER REFERENCES customers (customer_id)
);
INSERT INTO customers VALUES
(1, 'Asha Rao', 'Pune', NULL),
(2, 'Ben Okafor', 'Leeds', 1),
(3, 'Chen Wei', 'Pune', 1),
(4, 'Dana Cruz', 'York', 2),
(5, 'Eli Novak', 'Leeds', NULL),
(6, 'Farah Haddad', NULL, 3);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers (customer_id), -- NULL = guest checkout
placed_on DATE NOT NULL,
status VARCHAR(10) NOT NULL
);
INSERT INTO orders VALUES
(101, 1, DATE '2026-03-02', 'shipped'),
(102, 2, DATE '2026-03-05', 'shipped'),
(103, 1, DATE '2026-03-09', 'delivered'),
(104, 3, DATE '2026-03-15', 'pending'),
(105, 4, DATE '2026-03-20', 'cancelled'),
(106, NULL, DATE '2026-03-22', 'shipped');
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders (order_id),
product_id INTEGER REFERENCES products (product_id),
qty INTEGER NOT NULL,
unit_price DECIMAL(8, 2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);
INSERT INTO order_items VALUES
(101, 1, 1, 249.99), (101, 7, 3, 16.00),
(102, 3, 2, 34.50),
(103, 5, 1, 89.95), (103, 3, 1, 34.50), (103, 7, 2, 15.00),
(104, 2, 1, 489.00),
(105, 6, 1, 54.00),
(106, 7, 1, 16.00);
\pset null '(null)'
-- FULL JOIN: customers without orders AND orders without customers
SELECT c.name, o.order_id,
CASE
WHEN c.customer_id IS NULL THEN 'order without customer'
WHEN o.order_id IS NULL THEN 'customer without orders'
ELSE 'matched'
END AS match_status
FROM customers c
FULL JOIN orders o ON o.customer_id = c.customer_id
ORDER BY o.order_id, c.name;
-- RIGHT JOIN is LEFT JOIN with the tables swapped: these two count the same rows
SELECT COUNT(*) AS left_join_rows FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id;
SELECT COUNT(*) AS right_join_rows FROM orders o RIGHT JOIN customers c ON o.customer_id = c.customer_id;
name | order_id | match_status
--------------+----------+-------------------------
Asha Rao | 101 | matched
Ben Okafor | 102 | matched
Asha Rao | 103 | matched
Chen Wei | 104 | matched
Dana Cruz | 105 | matched
(null) | 106 | order without customer
Eli Novak | (null) | customer without orders
Farah Haddad | (null) | customer without orders
(8 rows)
left_join_rows
----------------
7
(1 row)
right_join_rows
-----------------
7
(1 row)
A FULL JOIN keeps the unmatched rows of both tables. Here it shows the five matched orders, the guest order 106 with NULL customer columns, and the two customers without orders. The CASE labels each kind of row by checking which side's key is NULL. FULL JOIN is the natural tool for comparing two lists, for example to find rows that exist in only one of two tables (article 17).
MySQL doesn't support FULL JOIN. The usual workaround combines a left join with the unmatched rows of a right join:
SELECT c.name, o.order_id
FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id
FROM customers c RIGHT JOIN orders o ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL;
The second half adds only the orders with no customer, so no row appears twice. (UNION ALL is covered in article 15.) SQLite added FULL JOIN and RIGHT JOIN in version 3.39.
A RIGHT JOIN is a LEFT JOIN with the tables written the other way round. orders o RIGHT JOIN customers c keeps every customer, just like customers c LEFT JOIN orders o, which is why both counts are 7. Most people write only LEFT JOIN, putting the table whose rows must all be kept first, because a chain of joins is easier to read when it always extends to the right.
Chains of joins
When a query joins several tables, every join after an outer join has to preserve the unmatched rows too:
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
category VARCHAR(20) NOT NULL,
brand VARCHAR(20),
price DECIMAL(8, 2) NOT NULL,
stock INTEGER NOT NULL,
weight_g INTEGER
);
INSERT INTO products VALUES
(1, 'Ridge 2 tent', 'tents', 'Alpina', 249.99, 12, 2100),
(2, 'Summit 4 tent', 'tents', 'Alpina', 489.00, 3, 4300),
(3, 'Glow head torch', 'lighting', 'Lumen', 34.50, 40, 90),
(4, 'Basecamp lantern', 'lighting', NULL, 27.00, 15, NULL),
(5, 'Trail 30 backpack', 'packs', 'Kestrel', 89.95, 22, 1150),
(6, 'Day 18 backpack', 'packs', 'Kestrel', 54.00, 0, 640),
(7, 'Merino socks', 'clothing', 'Woolly', 16.00, 75, 80);
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
city VARCHAR(30),
referred_by INTEGER REFERENCES customers (customer_id)
);
INSERT INTO customers VALUES
(1, 'Asha Rao', 'Pune', NULL),
(2, 'Ben Okafor', 'Leeds', 1),
(3, 'Chen Wei', 'Pune', 1),
(4, 'Dana Cruz', 'York', 2),
(5, 'Eli Novak', 'Leeds', NULL),
(6, 'Farah Haddad', NULL, 3);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers (customer_id), -- NULL = guest checkout
placed_on DATE NOT NULL,
status VARCHAR(10) NOT NULL
);
INSERT INTO orders VALUES
(101, 1, DATE '2026-03-02', 'shipped'),
(102, 2, DATE '2026-03-05', 'shipped'),
(103, 1, DATE '2026-03-09', 'delivered'),
(104, 3, DATE '2026-03-15', 'pending'),
(105, 4, DATE '2026-03-20', 'cancelled'),
(106, NULL, DATE '2026-03-22', 'shipped');
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders (order_id),
product_id INTEGER REFERENCES products (product_id),
qty INTEGER NOT NULL,
unit_price DECIMAL(8, 2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);
INSERT INTO order_items VALUES
(101, 1, 1, 249.99), (101, 7, 3, 16.00),
(102, 3, 2, 34.50),
(103, 5, 1, 89.95), (103, 3, 1, 34.50), (103, 7, 2, 15.00),
(104, 2, 1, 489.00),
(105, 6, 1, 54.00),
(106, 7, 1, 16.00);
\pset null '(null)'
-- Every product, with the orders it appears in (if any)
SELECT p.name, oi.order_id, o.placed_on
FROM products p
LEFT JOIN order_items oi ON oi.product_id = p.product_id
LEFT JOIN orders o ON o.order_id = oi.order_id
WHERE p.category IN ('lighting', 'packs')
ORDER BY p.name, oi.order_id;
-- Trap: an inner join after a left join drops the unmatched products again
SELECT p.name, oi.order_id, o.placed_on
FROM products p
LEFT JOIN order_items oi ON oi.product_id = p.product_id
JOIN orders o ON o.order_id = oi.order_id
WHERE p.category IN ('lighting', 'packs')
ORDER BY p.name, oi.order_id;
name | order_id | placed_on
-------------------+----------+------------
Basecamp lantern | (null) | (null)
Day 18 backpack | 105 | 2026-03-20
Glow head torch | 102 | 2026-03-05
Glow head torch | 103 | 2026-03-09
Trail 30 backpack | 103 | 2026-03-09
(5 rows)
name | order_id | placed_on
-------------------+----------+------------
Day 18 backpack | 105 | 2026-03-20
Glow head torch | 102 | 2026-03-05
Glow head torch | 103 | 2026-03-09
Trail 30 backpack | 103 | 2026-03-09
(4 rows)
The first query keeps every lighting and pack product. The lantern has never been ordered, so its order_items columns are NULL, and the second LEFT JOIN to orders finds no order for a NULL order_id, so its order columns are NULL as well.
In the second query, the second join is an inner JOIN. Its condition, o.order_id = oi.order_id, is unknown for the lantern's row, because oi.order_id is NULL, so the inner join drops the row. The lantern disappears, as if the first join had been an inner join too.
So once you've used a LEFT JOIN to keep rows, every later join that touches the optional tables must also be a LEFT JOIN. Alternatively, join the optional tables together first, in parentheses or a subquery, then left-join the result:
SELECT p.name, oi.order_id, o.placed_on
FROM products p
LEFT JOIN (order_items oi
JOIN orders o ON o.order_id = oi.order_id)
ON oi.product_id = p.product_id;
This version means something slightly different: an order line counts as a match only if its order exists. Here the foreign keys guarantee that, so the results are the same.
Outer joins and counting
Outer joins are usually combined with aggregates, as in "the number of orders per customer, including zero". Part 4 covers GROUP BY, but remember one detail now: count a column from the optional side, COUNT(o.order_id), not COUNT(*). COUNT(*) counts Eli's padded row as one order. COUNT(o.order_id) ignores its NULL and gives 0.
Oracle's (+) syntax
Before Oracle supported LEFT JOIN (in version 9i), outer joins were written in the comma syntax, with (+) after the columns of the table that may be missing:
SELECT c.name, o.order_id
FROM customers c, orders o
WHERE o.customer_id (+) = c.customer_id; -- same as customers c LEFT JOIN orders o
You'll still find it in older Oracle code. It has limitations: no full outer joins, and confusing rules when conditions involve several tables. Oracle recommends the standard JOIN syntax, and so does this course. SQL Server once had a similar *= operator, which was removed long ago.
What's current
LEFT,RIGHTandFULL JOINare standard and supported by PostgreSQL, Oracle, SQL Server and SQLite (3.39 and later). MySQL still supports onlyLEFTandRIGHT JOIN, notFULL JOIN.- Optimizers in all major products turn outer joins into inner joins when a
WHEREcondition rejects NULLs, and PostgreSQL, SQL Server and Oracle can remove a left join entirely when no column from the right table is used and the join can't add rows. These changes affect speed, never results.
Common questions
What's the difference between LEFT JOIN and LEFT OUTER JOIN?
None. OUTER is optional.
Why did my LEFT JOIN return the same rows as an inner join?
Usually because a WHERE condition tests a column from the right table, such as WHERE o.status = 'shipped', which removes the NULL-padded rows. Move the condition into the ON clause. An inner join later in the chain has the same effect.
Can a LEFT JOIN return more rows than the left table has? Yes. A left row with three matches appears three times. A left join guarantees that every left row appears at least once, not exactly once.
How do I find rows in table A that aren't in table B?
A LEFT JOIN B ON ... WHERE B.key IS NULL, or WHERE NOT EXISTS (SELECT 1 FROM B WHERE ...), or EXCEPT for whole rows. Article 16 compares them.
How do I show 0 instead of NULL for the missing side?
Wrap the column in COALESCE, as in COALESCE(SUM(oi.qty * oi.unit_price), 0) or COALESCE(o.some_column, 0). Do it in the select list, not in the join condition.
Which table should come first in a LEFT JOIN?
The table whose rows must all appear, such as all customers or all products. Write it first, and extend the chain to the right with more LEFT JOINs.
Summary
- An outer join keeps rows that have no match and fills the other side's columns with NULL.
LEFTkeeps all rows of the first table,RIGHTall rows of the second, andFULLall rows of both. - A left row with several matches still appears several times. An outer join guarantees at least one row per preserved row.
LEFT JOIN ... WHERE right.key IS NULLfinds rows without a partner (an anti-join).- Conditions on the optional table that define a match belong in
ON. AWHEREcondition on the optional table turns the outer join into an inner join. - After a
LEFT JOIN, later joins to optional tables must also be outer joins, or the unmatched rows disappear. RIGHT JOINis a swappedLEFT JOIN. PreferLEFT JOINfor readability.- MySQL has no
FULL JOIN: combine aLEFT JOINand the unmatched rows of aRIGHT JOINwithUNION ALL. - Count the optional side's key,
COUNT(o.order_id), to get zero for rows with no match.
Practice questions
Write a query that lists every product with the order lines it appears in, showing NULL for products that have never been ordered. Then change it to list only the products that have never been ordered.
Answer
SELECT p.name, oi.order_id, oi.qty
FROM products p
LEFT JOIN order_items oi ON oi.product_id = p.product_id
ORDER BY p.name, oi.order_id;
To keep only the products that have never been ordered, add a test for the missing side:
SELECT p.name
FROM products p
LEFT JOIN order_items oi ON oi.product_id = p.product_id
WHERE oi.order_id IS NULL;
The only product returned is the Basecamp lantern. oi.order_id is part of order_items' primary key, so it's never NULL in a real match.
A report should list all customers with their orders placed in March 2026 or later. It uses LEFT JOIN orders o ON o.customer_id = c.customer_id WHERE o.placed_on >= DATE '2026-03-01'. Eli and Farah are missing. Why, and what's the fix?
Answer
The WHERE condition is evaluated after the join. For Eli and Farah, o.placed_on is NULL, so NULL >= DATE '2026-03-01' is unknown and their rows are removed, which makes the query behave like an inner join. Move the condition into the join: LEFT JOIN orders o ON o.customer_id = c.customer_id AND o.placed_on >= DATE '2026-03-01'.
How many rows does SELECT * FROM customers c FULL JOIN orders o ON o.customer_id = c.customer_id return, and why?
Answer
8 rows: the 5 matched pairs (orders 101 to 105, each with its customer), 2 customers without orders (Eli and Farah, with NULL order columns) and 1 order without a customer (the guest order 106, with NULL customer columns).
What's wrong with this query, which is meant to list every customer with the products they bought?
SELECT c.name, p.name AS product
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
JOIN order_items oi ON oi.order_id = o.order_id
LEFT JOIN products p ON p.product_id = oi.product_id;
Answer
The inner join to order_items drops the customers without orders. For Eli and Farah, o.order_id is NULL, so oi.order_id = o.order_id is never true and their rows disappear. Make that join a LEFT JOIN too, so that every join after the first outer join preserves the unmatched rows.
Previous: SQL Inner Joins: Combining Rows From Related Tables

More in SQL Course
Quiz: SQL Dialect Differences (PostgreSQL, MySQL, SQL Server, Oracle, SQLite)
Twenty-four questions on how the major SQL databases differ: limiting rows, concatenation, empty strings, identifier case, upserts, identity columns, transactional DDL, error handling, isolation defaults, booleans, set operations, RETURNING, dates and typing.
Quiz: SQL Delicate Details
Twenty-five questions on SQL's subtle corners: NULLs in aggregates, IN and CHECK, sort order of NULLs, rounding, CHAR padding, window frame defaults, identity gaps, LIKE escapes, empty groups, set operations and multi-column updates.
Quiz: SQL Interview Questions
Thirty SQL questions of the kind asked in developer and analyst interviews: WHERE vs HAVING, DELETE vs TRUNCATE, keys, joins, duplicates, Nth-highest values, window functions, ACID, isolation, locking, indexes, views, procedures and performance.
Quiz: SQL Course Final Exam (All 15 Parts)
The final exam for the SQL course: thirty questions across all fifteen parts, from query processing, NULLs, joins and aggregation through windows, data changes, types, design, indexes, security, transactions, procedural SQL and tuning.
Discussion
Sign in to ask a question or share what you learned.