SQL Joins · Lesson 14 of 30
SQL LEFT JOIN, RIGHT JOIN and FULL JOIN
An inner join silently drops rows that have no partner – the customer who never ordered, the category with no products. Often those are exactly the rows you care about: inactive customers to email, empty categories to fill. Outer joins keep them, filling the missing side with NULL.
In this lesson
- LEFT JOIN: keep every row from the left table
- Finding rows with no match (anti-join)
- The WHERE trap that turns a LEFT JOIN into an INNER JOIN
- RIGHT JOIN and FULL OUTER JOIN
Join types at a glance (A = left table, B = right table)
- INNERonly matching rows
- LEFTall of A + matches from B
- RIGHTall of B + matches from A
- FULLall of A and all of B
- LEFT … IS NULLA rows with no match
- CROSSevery A with every B
LEFT JOIN
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
WHERE c.customer_id IN (1, 6, 7, 8)
ORDER BY c.customer_id, o.order_id;
+-------------+-------------+----------+-----------+ | customer_id | name | order_id | status | +-------------+-------------+----------+-----------+ | 1 | Asha Verma | 101 | delivered | | 1 | Asha Verma | 103 | delivered | | 1 | Asha Verma | 109 | shipped | | 6 | Arjun Singh | 108 | pending | | 7 | Neha Gupta | 110 | delivered | | 8 | Vikram Das | NULL | NULL | +-------------+-------------+----------+-----------+
Every customer on the left appears at least once. Vikram has no orders, so the order columns are NULL. Asha has three orders, so she appears three times.
Counting with LEFT JOIN: include the zeros
SELECT cat.name AS category, COUNT(p.product_id) AS products
FROM categories cat
LEFT JOIN products p ON p.category_id = cat.category_id
GROUP BY cat.category_id, cat.name
ORDER BY products DESC, category;
+-------------+----------+ | category | products | +-------------+----------+ | Electronics | 4 | | Books | 3 | | Stationery | 2 | | Fitness | 0 | +-------------+----------+
COUNT(column), not COUNT(*)
For Fitness the joined row has p.product_id = NULL. COUNT(p.product_id) skips NULL and correctly gives 0; COUNT(*) would count the row and wrongly report 1.
Finding rows with no match
LEFT JOIN, then keep only rows where the right side is NULL. This "anti-join" answers questions like "customers who never ordered":
SELECT c.name, c.email
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
+------------+--------------------+ | name | email | +------------+--------------------+ | Vikram Das | vikram@example.com | +------------+--------------------+
-- Employees who manage nobody (no one has them as manager)
SELECT e.name, e.department
FROM employees e
LEFT JOIN employees r ON r.manager_id = e.emp_id
WHERE r.emp_id IS NULL
ORDER BY e.emp_id;
+--------------+-------------+ | name | department | +--------------+-------------+ | Sana Iqbal | Engineering | | Dev Patel | Engineering | | Priya Sharma | Engineering | | Lata Joshi | Sales | | Tom Mathew | Sales | | Imran Shaikh | HR | +--------------+-------------+
The WHERE trap
Suppose you want every customer with their delivered orders only. Putting the condition in WHERE removes the customers without delivered orders – the NULL rows fail the test:
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.status = 'delivered' -- turns the LEFT JOIN into an INNER JOIN
AND c.customer_id IN (1, 5, 6, 8)
ORDER BY c.customer_id, o.order_id;
+------------+----------+ | name | order_id | +------------+----------+ | Asha Verma | 101 | | Asha Verma | 103 | +------------+----------+
Only Asha is left. Zoya's only order is shipped, Arjun's is pending and Vikram has none, so their NULL or non-matching rows fail the WHERE test and vanish. Move the condition into ON, so it decides which orders match instead of which rows survive:
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id AND o.status = 'delivered'
WHERE c.customer_id IN (1, 5, 6, 8)
ORDER BY c.customer_id, o.order_id;
+-------------+----------+ | name | order_id | +-------------+----------+ | Asha Verma | 101 | | Asha Verma | 103 | | Zoya Khan | NULL | | Arjun Singh | NULL | | Vikram Das | NULL | +-------------+----------+
Where does a condition on the right table go?
LEFT JOIN … condition on B
- In ONfilters which B rows match; every A row is still kept
- In WHEREfilters the final rows; A rows with NULL B are removed
RIGHT JOIN
A RIGHT JOIN B keeps every row of B. It's the same as B LEFT JOIN A, so most teams write only LEFT JOINs for consistency:
SELECT p.name AS product, cat.name AS category
FROM products p
RIGHT JOIN categories cat ON cat.category_id = p.category_id
WHERE cat.category_id IN (3, 4);
+--------------+------------+ | product | category | +--------------+------------+ | Gel Pen Pack | Stationery | | A5 Notebook | Stationery | | NULL | Fitness | +--------------+------------+
FULL OUTER JOIN
A full join keeps unmatched rows from both sides – here, the product without a category and the category without products. PostgreSQL, SQL Server and Oracle support FULL OUTER JOIN; MySQL doesn't, so combine a LEFT and a RIGHT join with UNION:
SELECT p.name AS product, cat.name AS category
FROM products p LEFT JOIN categories cat ON cat.category_id = p.category_id
WHERE cat.category_id IS NULL
UNION
SELECT p.name, cat.name
FROM products p RIGHT JOIN categories cat ON cat.category_id = p.category_id
WHERE p.product_id IS NULL;
+-----------+----------+ | product | category | +-----------+----------+ | Desk Lamp | NULL | | NULL | Fitness | +-----------+----------+
For brevity this shows only the unmatched rows from each side; drop the two WHERE clauses to get the complete full join.
Practice
Try it yourself
Show every customer with their number of orders, including customers with none.
Show solution
SELECT c.name, COUNT(o.order_id) AS orders FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id GROUP BY c.customer_id, c.name ORDER BY orders DESC, c.name;+-------------+--------+ | name | orders | +-------------+--------+ | Asha Verma | 3 | | Kiran Rao | 2 | | Meera Nair | 2 | | Ravi Kumar | 2 | | Arjun Singh | 1 | | Neha Gupta | 1 | | Zoya Khan | 1 | | Vikram Das | 0 | +-------------+--------+
Find categories that have no products.
Show solution
SELECT cat.name FROM categories cat LEFT JOIN products p ON p.category_id = cat.category_id WHERE p.product_id IS NULL;+---------+ | name | +---------+ | Fitness | +---------+
Quick check
What fills the right-side columns when a LEFT JOIN finds no match?
NULL.
Why can a WHERE condition break a LEFT JOIN?
A condition on the right table fails for NULL rows, removing the unmatched left rows – put it in ON instead.
How do you write a FULL OUTER JOIN in MySQL?
LEFT JOIN UNION RIGHT JOIN.
Summary
- LEFT JOIN keeps all left rows; missing right columns become NULL.
- LEFT JOIN … WHERE right.key IS NULL finds rows with no match; count with COUNT(right.col).
- Conditions on the right table belong in ON; FULL JOIN in MySQL = LEFT UNION RIGHT.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.