4.2 RIGHT, FULL OUTER, SELF JOIN & CROSS JOIN
Master every remaining SQL JOIN type: SELF JOIN for employee-manager hierarchies and pair comparisons, FULL OUTER JOIN for reconciliation (plus MySQL workaround), and CROSS JOIN for generating combinations.
1. Complete Comparison of All 5 SQL JOINs
| Join Type | Keeps Unmatched Left? | Keeps Unmatched Right? | Real-World Use Case |
|---|---|---|---|
INNER JOIN | ❌ | ❌ | Orders with valid customer & product info |
LEFT JOIN | ✅ | ❌ | All customers + their orders (including 0-order customers) |
RIGHT JOIN | ❌ | ✅ | Mirror image of LEFT JOIN (almost everyone rewrites it as LEFT JOIN for readability!) |
FULL OUTER JOIN | ✅ | ✅ | Reconciling two systems (e.g., Bank ledger vs Stripe payouts to find mismatches on either side) |
CROSS JOIN | Cartesian Product (M × N) | Cartesian Product (M × N) | Generating all (date × store) or (size × color) SKU combinations |
2. SELF JOIN — Joining a Table to Itself (Huge Interview Favorite!)
A SELF JOIN isn't a special keyword — it's simply an INNER or LEFT JOIN where the same table appears twice under two different aliases (e and m).
Classic #1: Employee & Manager Hierarchy
Every employee row has a manager_id pointing to another row's emp_id in the same employees table:
-- Show every employee alongside their manager's name (CEO has manager_id = NULL -> LEFT JOIN!)
SELECT
e.name AS employee_name,
COALESCE(m.name, 'CEO (No Manager)') AS manager_name,
e.salary AS emp_salary,
m.salary AS mgr_salary
FROM employees e
LEFT JOIN employees m
ON e.manager_id = m.emp_id;
Classic #2: Employees Who Earn MORE Than Their Manager (LeetCode #181!)
SELECT e.name AS employee
FROM employees e
INNER JOIN employees m
ON e.manager_id = m.emp_id
WHERE e.salary > m.salary;
Watch SELF JOIN Match Employees to Managers Live
| emp_id | name | manager_id |
|---|---|---|
| 1 | Priya | NULL |
| 2 | Aarav | 1 |
| 3 | Meera | 2 |
3. FULL OUTER JOIN (Data Reconciliation + MySQL Workaround)
FULL OUTER JOIN keeps every row from both tables, padding NULL on whichever side didn't match. Perfect for auditing payments vs invoices:
-- PostgreSQL / SQL Server / Oracle: Find mismatched invoices & payments
SELECT
COALESCE(i.invoice_id, p.invoice_id) AS invoice_id,
i.amount AS invoiced_amt,
p.amount AS paid_amt
FROM invoices i
FULL OUTER JOIN payments p
ON i.invoice_id = p.invoice_id
WHERE i.invoice_id IS NULL OR p.invoice_id IS NULL;
MySQL Difference: MySQL does not have native
FULL OUTER JOINsyntax! You emulate it cleanly withLEFT JOIN ... UNION RIGHT JOIN ...(which you'll master in Lesson 4.3).
4. CROSS JOIN — Generating All Combinations (M × N)
A CROSS JOIN has no ON clause — it pairs every row of Table A with every row of Table B:
-- Generate every (size × color) product SKU (3 sizes × 4 colors = 12 SKUs)
SELECT s.size_name, c.color_name
FROM sizes s
CROSS JOIN colors c;
🚨 Production Warning: Accidentally omitting
ONon two 100,000-row tables produces10,000,000,000(10 billion) rows and can crash your database! Always check your row counts before running aCROSS JOIN.
5. Chaining 3+ Tables Cleanly
-- Stitch users -> orders -> order_items -> products in one readable pipeline
SELECT
u.name AS customer,
o.order_id,
p.product_name,
oi.quantity,
oi.unit_price
FROM users u
INNER JOIN orders o ON u.user_id = o.user_id
INNER JOIN order_items oi ON o.order_id = oi.order_id
INNER JOIN products p ON oi.product_id = p.product_id
WHERE o.status = 'paid';
6. Quick Check Your Understanding
Module 4.2 Quick Quiz
Keep Exploring: Related Lessons & Tools
All Courses →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.
Test regular expressions live with color-coded match highlights, capture group inspection, plain-English token breakdown, find-and-replace, and Python/JS snippets.