🟢 Day 4 — JOINs (Combining Tables the Right Way)
🎯 Day 4 Outcome
By the end of Day 4, the student should be able to:
Understand why JOINs exist Use INNER JOIN and LEFT JOIN correctly Explain JOIN logic in plain English Avoid the most common JOIN mistakes JOINs are not hard.
They are just matching rows across tables.
1️⃣ Why JOINs Exist (Teaching Script)
Start With This Question
“We stored users and orders in separate tables.
How do we see user name with order amount?”
Answer:
We combine rows from two tables This operation is called a JOIN.
2️⃣ Tables We’ll Use Today
users (
user_id INT,
name VARCHAR(100),
email VARCHAR(100)
)
orders (
order_id INT,
user_id INT,
amount INT,
order_date DATE
)
3️⃣ INNER JOIN — Only Matching Rows
Mental Model (Very Important)
INNER JOIN = intersection
Only rows that exist in both tables are returned.
Syntax
SELECT columns
FROM table1
INNER JOIN table2
ON condition;
Example 1 — Orders with User Names
SELECT u.name, o.amount
FROM users u
INNER JOIN orders o
ON u.user_id = o.user_id;
Read in English:
“For each order, find the user whose user_id matches, and show name + amount”
What Happens Internally
For each row in orders:
Database looks for matching user_id in users If found → row is returned 4️⃣ LEFT JOIN — Keep Everything from Left Table
Mental Model
LEFT JOIN = everything from left table + matches from right
Even if there is no match, left table row stays.
Syntax
SELECT columns
FROM table1
LEFT JOIN table2
ON condition;
Example 2 — Users with Their Orders (If Any)
SELECT u.name, o.amount
FROM users u
LEFT JOIN orders o
ON u.user_id = o.user_id;
What this returns:
Users with orders → normal rows Users without orders → amount = NULL 5️⃣ Most Asked Interview Question
❓ “Find users who have NOT placed any orders”
This is where LEFT JOIN shines.
Solution
SELECT u.user_id, u.name
FROM users u
LEFT JOIN orders o
ON u.user_id = o.user_id
WHERE o.order_id IS NULL;
Read carefully:
LEFT JOIN keeps all users Users without orders → order_id becomes NULL This question appears in almost every SQL interview.
6️⃣ INNER vs LEFT JOIN (Clear Comparison)
7️⃣ Aliases (u, o) — Not Optional in Interviews
Bad:
users.user_id = orders.user_id
Good:
Why interviewers expect aliases:
Required in multi-join queries 8️⃣ JOIN + GROUP BY (Very Common)
Example — Total Spend Per User (Including Zero)
SELECT u.user_id, u.name, COALESCE(SUM(o.amount), 0) AS total_spent
FROM users u
LEFT JOIN orders o
ON u.user_id = o.user_id
GROUP BY u.user_id, u.name;
Explain:
LEFT JOIN keeps all users COALESCE converts NULL → 0 9️⃣ Common JOIN Mistakes (Read Carefully)
❌ Forgetting ON condition
❌ Using WHERE instead of ON incorrectly
❌ Using INNER JOIN when LEFT JOIN needed
❌ Filtering NULLs incorrectly
❌ Forgetting GROUP BY columns
🔟 Practice Problems (DO THESE)
Problem 1 — Basic JOIN
Write SQL to get:
Problem 2 — Users with No Orders
Write SQL to find:
For users who never placed an order.
Problem 3 — Total Orders Per User
Include users with zero orders.
Problem 4 — High Spenders
Find users whose total order amount > 1000.
🔍 HINTS (Read Only If Stuck)
Combine tables using matching IDs LEFT JOIN keeps all rows from left table Use IS NULL to detect missing matches Use GROUP BY when aggregating ✅ Solutions (Read After Attempting)
Solution — Problem 1
SELECT u.name, o.amount, o.order_date
FROM users u
INNER JOIN orders o
ON u.user_id = o.user_id;
Solution — Problem 2
SELECT u.user_id, u.name
FROM users u
LEFT JOIN orders o
ON u.user_id = o.user_id
WHERE o.order_id IS NULL;
Solution — Problem 3
SELECT u.user_id, u.name, COUNT(o.order_id) AS order_count
FROM users u
LEFT JOIN orders o
ON u.user_id = o.user_id
GROUP BY u.user_id, u.name;
Solution — Problem 4
SELECT u.user_id, u.name, SUM(o.amount) AS total_spent
FROM users u
INNER JOIN orders o
ON u.user_id = o.user_id
GROUP BY u.user_id, u.name
HAVING SUM(o.amount) > 1000;
🧠 Reflection Questions (Must Answer)
Why does LEFT JOIN return more rows than INNER JOIN? Why is IS NULL used instead of = NULL? When would INNER JOIN be wrong? Why does interviewers love “users with no orders” question? ⭐ Day 4 Golden Rule
JOINs are just matching IDs,
not magic.