LEFT, RIGHT & FULL JOIN
Keep all rows from one side (or both sides) even when there is no match on the other — essential for finding missing data.
LEFT JOIN keeps every row from the left table. RIGHT JOIN keeps every row from the right. FULL JOIN keeps all rows from both.
EXERCISE
1LEFT JOIN — Keep All Left Rows
LEFT JOIN returns every row from the left table, and matching rows from the right table. If no match exists, the right side columns come back as NULL.
LEFT JOIN vs INNER JOIN:
-- INNER JOIN: only matched rows (Rohan excluded)
SELECT c.name, o.id AS order_id
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id;
-- LEFT JOIN: all customers, NULL for orders columns where no match:
SELECT c.name, o.id AS order_id
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id;
-- Output:
-- name | order_id
-- Arjun | 1
-- Arjun | 2
-- Priya | 3
-- Rohan | NULL ← kept because of LEFT JOIN
Finding rows with NO match — the most common LEFT JOIN pattern:
-- Customers who have NEVER placed an order:
SELECT c.name, c.email
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
WHERE o.id IS NULL; -- NULL here means: no matching order row
LEFT JOIN with aggregation — include zero counts:
-- Order count per customer (include customers with 0 orders):
SELECT
c.name,
COUNT(o.id) AS order_count -- COUNT(o.id) returns 0 for NULLs, COUNT(*) returns 1
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
GROUP BY c.id, c.name
ORDER BY order_count DESC;
Pro Tip:
COUNT(*)counts the row even if all joined columns are NULL — giving 1 for customers with no orders.COUNT(o.id)only counts non-NULL values — giving 0. Always useCOUNT(joined_table.id)with LEFT JOIN + GROUP BY.
EXERCISE
2RIGHT JOIN, FULL JOIN & WHERE in LEFT JOINs
RIGHT JOIN mirrors LEFT JOIN. FULL JOIN (not native in MySQL) keeps all rows from both sides. And WHERE placement in LEFT JOINs changes everything.
RIGHT JOIN:
-- Keep all rows from the RIGHT table (orders), match customers where possible:
SELECT c.name, o.id AS order_id, o.total
FROM customers c
RIGHT JOIN orders o ON c.id = o.customer_id;
-- Any order without a matching customer_id would appear with NULL for c.name
-- In practice, RIGHT JOIN is rare — most people flip the table order and use LEFT JOIN
FULL OUTER JOIN (MySQL workaround):
MySQL does not support FULL OUTER JOIN directly. Simulate it with UNION:
-- All customers AND all orders (matched and unmatched):
SELECT c.name, o.id AS order_id
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
UNION
SELECT c.name, o.id AS order_id
FROM customers c
RIGHT JOIN orders o ON c.id = o.customer_id;
Critical: WHERE vs ON filtering in LEFT JOIN:
-- Filter in ON — keeps all left rows, filters what matches on right:
SELECT c.name, o.total
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
AND o.status = 'completed';
-- ALL customers returned; right side only shows completed orders (others NULL)
-- Filter in WHERE — turns it back into an INNER JOIN:
SELECT c.name, o.total
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
WHERE o.status = 'completed'; -- customers with no completed orders are EXCLUDED
Pro Tip: This is one of the most common interview gotchas. Putting a RIGHT-table condition in WHERE instead of ON silently converts your LEFT JOIN to an INNER JOIN. If you want all left rows AND a filter on the right, put the filter in ON.
EXERCISE
3Choosing the Right JOIN Type
Picking the right JOIN is not about syntax — it is about understanding what question you are asking and what happens to unmatched rows.
The decision framework:
"Do I want rows that have no match?"
NO → INNER JOIN (only matched pairs)
YES, from left table → LEFT JOIN
YES, from right table → RIGHT JOIN (or flip tables and LEFT JOIN)
YES, from both tables → FULL JOIN (UNION workaround in MySQL)
Summary of all JOIN types:
| JOIN Type | What it returns |
|---|---|
| INNER JOIN | Only rows with a match in BOTH tables |
| LEFT JOIN | All rows from left + matching from right (NULL if no match) |
| RIGHT JOIN | All rows from right + matching from left (NULL if no match) |
| FULL JOIN | All rows from both tables (NULL where no match on either side) |
| CROSS JOIN | Every combination of left × right rows (no ON condition) |
| SELF JOIN | Table joined to itself (for hierarchical data) |
When each matters:
-- INNER: only customers who have orders
FROM customers INNER JOIN orders ON ...
-- LEFT: all products, even those never ordered (sales report):
FROM products LEFT JOIN order_items ON ...
-- LEFT + IS NULL: products that have NEVER been ordered (find dead stock):
FROM products p LEFT JOIN order_items oi ON p.id = oi.product_id
WHERE oi.id IS NULL
-- SELF: employees and their manager (same table):
FROM employees e LEFT JOIN employees m ON e.manager_id = m.id
Pro Tip: In real work, LEFT JOIN is used far more than RIGHT JOIN — because it reads naturally ("keep all from the main table, add what we can from the secondary table"). RIGHT JOIN is usually rewritten as a LEFT JOIN with swapped table order.
