INNER JOIN: only matching rows
SELECT o.order_id, c.name, c.city, o.status
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
WHERE o.order_date < '2026-04-08'
ORDER BY o.order_id;| order_id | name | city | status |
|---|---|---|---|
| 101 | Asha | Kolkata | DELIVERED |
| 102 | Rahul | Mumbai | DELIVERED |
| 103 | Asha | Kolkata | DELIVERED |
| 104 | Priya | Bengaluru | CANCELLED |
| 105 | Imran | Delhi | DELIVERED |
o and c are aliases: short names so you can write o.order_id. Always say which table a column comes from once you have more than one.
The join types in one picture
INNER JOIN
Only customers WITH orders
Asha + her orders
Rahul + his orders
Sara ✗ (no orders)
LEFT JOIN
ALL customers; NULLs if no orders
Asha + her orders
Rahul + his orders
Sara + NULL
ANTI JOIN
Customers WITHOUT orders
Sara
| Join | Keeps | Typical use |
|---|---|---|
| INNER JOIN | Rows matching on both sides | Orders with their customer details |
| LEFT JOIN | All left rows + matches (else NULL) | Every customer, even with 0 orders |
| RIGHT JOIN | All right rows + matches | Rare: swap the tables and use LEFT |
| FULL OUTER JOIN | All rows from both sides | Reconciling two systems |
| CROSS JOIN | Every combination | All customers × all months |
| Self join | A table joined to itself | Employee next to manager |
| Semi / anti join | Rows that do / don't have a match | EXISTS / NOT EXISTS |
LEFT JOIN: keep everyone
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 | 3 |
| Rahul | 3 |
| Arjun | 2 |
| Imran | 2 |
| Meera | 2 |
| Priya | 2 |
| Vikram | 1 |
| Sara | 0 |
Sara appears with 0 orders. COUNT(o.order_id) counts only matched orders; COUNT(*) would wrongly give her 1.
Anti joins: find what's missing
-- Customers who never ordered: LEFT JOIN, keep rows with no match
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;| name |
|---|
| Sara |
-- Products never sold: NOT EXISTS (often the clearest and fastest)
SELECT p.name
FROM products p
WHERE NOT EXISTS (
SELECT 1 FROM order_items oi WHERE oi.product_id = p.product_id
);| name |
|---|
| Bluetooth Speaker |
Finding missing data (customers who never ordered, products never sold, orders without payments) is a daily data engineering task.
Joining three tables
SELECT p.category,
SUM(oi.quantity * oi.unit_price) AS delivered_revenue
FROM order_items oi
JOIN orders o ON o.order_id = oi.order_id
JOIN products p ON p.product_id = oi.product_id
WHERE o.status = 'DELIVERED'
GROUP BY p.category
ORDER BY delivered_revenue DESC;| category | delivered_revenue |
|---|---|
| Electronics | 27992.00 |
| Accessories | 9184.00 |
Each JOIN adds one table. Here: item → its order (for status) → its product (for category).
Self join: a table joined to itself
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON m.emp_id = e.manager_id
ORDER BY e.emp_id;| employee | manager |
|---|---|
| Kavya | NULL |
| Rohan | Kavya |
| Neha | Kavya |
| Amit | Rohan |
| Divya | Rohan |
| Karan | Rohan |
| Pooja | Neha |
| Farhan | Neha |
| Ishaan | Neha |
We use LEFT JOIN so Kavya, the CEO, stays in the list with no manager.
Trap 1: a WHERE that breaks your LEFT JOIN
-- ❌ Filtering the RIGHT table in WHERE turns the LEFT JOIN into an INNER JOIN
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_date >= '2026-05-01'
ORDER BY c.customer_id;| name | order_id |
|---|---|
| Asha | 109 |
| Rahul | 112 |
| Priya | 110 |
| Imran | 111 |
| Meera | 115 |
| Arjun | 113 |
| Vikram | 114 |
Sara vanished. For her, o.order_date is NULL, and NULL >= '2026-05-01' isn't true, so WHERE removed her. Your LEFT JOIN silently became an INNER JOIN.
-- ✅ Put the right table's filter in ON: every customer stays
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
AND o.order_date >= '2026-05-01'
ORDER BY c.customer_id;| name | order_id |
|---|---|
| Asha | 109 |
| Rahul | 112 |
| Priya | 110 |
| Imran | 111 |
| Meera | 115 |
| Arjun | 113 |
| Sara | NULL |
| Vikram | 114 |
Trap 2: fan-out (inflated numbers)
-- ❌ Joining to items repeats each order once per item: order count is inflated
SELECT c.name, COUNT(*) AS orders
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
JOIN order_items oi ON oi.order_id = o.order_id
WHERE c.name = 'Asha'
GROUP BY c.name;| name | orders |
|---|---|
| Asha | 4 |
-- ✅ Count distinct orders
SELECT c.name, COUNT(DISTINCT o.order_id) AS orders
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
JOIN order_items oi ON oi.order_id = o.order_id
WHERE c.name = 'Asha'
GROUP BY c.name;| name | orders |
|---|---|
| Asha | 3 |
Asha has 3 orders, but order 101 has 2 items, so the join produced 4 rows. Any SUM of order-level values (like a delivery fee) would also be inflated. Fix: count DISTINCT keys, or aggregate items per order first and then join.
CROSS JOIN: every combination
SELECT c.name, m.month
FROM customers c
CROSS JOIN (VALUES ('2026-04'), ('2026-05')) AS m(month)
WHERE c.customer_id <= 2
ORDER BY c.name, m.month;| name | month |
|---|---|
| Asha | 2026-04 |
| Asha | 2026-05 |
| Rahul | 2026-04 |
| Rahul | 2026-05 |
Useful for building “every customer × every month” grids that you then LEFT JOIN facts onto (so empty months show 0).
🎯 Interview questions on this topic
Try to answer out loud first, then tap to check.
Q1Explain INNER, LEFT, RIGHT and FULL OUTER joins.Fresher+
Q2How do you find customers who never placed an order?Fresher+
Q3Table A has 3 rows with key 1, table B has 2 rows with key 1. How many rows does an inner join on key return?1–3 yrs+
Q4Why might a LEFT JOIN return fewer rows than the left table?1–3 yrs+
Q5How would you reconcile two systems' order tables?3+ yrs+
Q6What join algorithms do databases use?3+ yrs+
Key takeaways
- ✓Joins combine tables on matching keys; pick INNER or LEFT on purpose.
- ✓Anti joins find missing data, a daily data engineering need.
- ✓Watch for the WHERE-on-LEFT-JOIN trap and fan-out duplicates.
- ✓Next: breaking hard questions into steps with subqueries and CTEs.
Frequently asked questions
What is a JOIN in SQL?
A JOIN combines rows from two tables using a matching condition, usually a foreign key equal to a primary key: orders.customer_id = customers.customer_id.
INNER JOIN or LEFT JOIN: which should I use?
INNER JOIN keeps only rows that match on both sides. LEFT JOIN keeps every row from the left table and fills the right side with NULLs when there's no match. Use LEFT when missing matches matter, like customers with zero orders.
NOT EXISTS, NOT IN or LEFT JOIN … IS NULL?
All can find rows with no match. NOT EXISTS is the safest and usually fastest. NOT IN breaks when the subquery returns a NULL. LEFT JOIN … IS NULL works too and is common.
Why did my join create duplicate rows?
Joining to a table with many matching rows (one order, many items) repeats the left row once per match. Aggregate at the right level first, or count DISTINCT keys.
What is a self join?
Joining a table to itself with two aliases, e.g. employees e and employees m to put each employee next to their manager.
🧠 Quick check: SQL joins
1. Sara has no orders. Which join keeps her in the result?
2. You LEFT JOIN orders and then WHERE o.order_date >= '2026-05-01'. Customers with no May orders...
3. Products that were never ordered: best pattern?
4. COUNT(*) after joining orders to order_items counts...
Checking your access…