4.1 INNER JOIN vs LEFT JOIN & The Anti-Join Pattern
Master the two JOINs you will use in 95% of real-world SQL queries: INNER JOIN vs LEFT JOIN, the Left Anti-Join pattern (find users with 0 orders), and the #1 LEFT JOIN + WHERE trap.
1. Visual Overview of SQL JOIN Types
Because relational databases split data cleanly across tables (users, orders, products), JOIN is how you stitch related rows back together on a matching key (u.user_id = o.user_id).
2. INNER JOIN vs LEFT JOIN — Side-by-Side Comparison
| Feature | INNER JOIN | LEFT JOIN (or LEFT OUTER JOIN) |
|---|---|---|
| Unmatched Left Rows | ❌ Dropped from output | ✅ Kept (right-table columns become NULL) |
| Unmatched Right Rows | ❌ Dropped | ❌ Dropped |
| Row Count | <= smaller matched set (or more if 1-to-many!) | Always >= number of rows in Left table |
| Classic Use Case | "Show all orders with their customer's name" | "Show ALL users and their total spend (even ₹0 spenders!)" |
-- INNER JOIN: Returns ONLY users who have placed at least 1 order
SELECT u.user_id, u.name, o.order_id, o.amount
FROM users u
INNER JOIN orders o
ON u.user_id = o.user_id;
-- LEFT JOIN: Returns ALL users — including users with 0 orders (amount = NULL)
SELECT
u.user_id,
u.name,
COUNT(o.order_id) AS order_count, -- COUNT(col) gives 0 for NULL!
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
ORDER BY total_spent DESC;
Interview Trap: In a
LEFT JOIN ... GROUP BY, useCOUNT(o.order_id), NOTCOUNT(*)!
Why? For a user with 0 orders,LEFT JOINproduces 1 row witho.order_id = NULL.COUNT(*)would wrongly say1 order, whileCOUNT(o.order_id)correctly ignoresNULLand returns0!
3. Watch LEFT JOIN Match Rows & Fill NULLs Step-by-Step
| u.user_id | u.name |
|---|---|
| 1 | Aarav |
| 2 | Meera |
| 3 | Kabir |
4. The #1 LEFT JOIN Bug: Filter in ON vs Filter in WHERE!
Suppose you want ALL users alongside their 2026 paid orders (showing NULL if they had no paid orders in 2026):
-- ❌ BUGGY: Filtering o.status in WHERE silently turns your LEFT JOIN into an INNER JOIN!
-- Why? Unmatched users have o.status = NULL, and NULL = 'paid' is UNKNOWN -> row dropped!
SELECT u.name, o.order_id, o.amount
FROM users u
LEFT JOIN orders o ON u.user_id = o.user_id
WHERE o.status = 'paid';
-- ✅ FIXED: Put the right-table filter inside the ON clause!
SELECT u.name, o.order_id, o.amount
FROM users u
LEFT JOIN orders o
ON u.user_id = o.user_id
AND o.status = 'paid';
Where You Put o.status = 'paid' | Effect on Unmatched Users | Result |
|---|---|---|
Inside WHERE | Drops every row where o.status IS NULL | Secretly behaves like INNER JOIN! |
Inside ON ... AND | Filters orders during matching, keeps all users | True LEFT JOIN (unmatched users stay with NULL) |
5. Quick Check Your Understanding
Module 4.1 Quick Quiz
Keep Exploring: Related Lessons & Tools
All Courses →Test regular expressions live with color-coded match highlights, capture group inspection, plain-English token breakdown, find-and-replace, and Python/JS snippets.
Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.