SQL · Module 04: Mastering JOINs & Set OperationsTutorial 11 of 24
All Tutorials·~1.5 hrs

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.

01 Intuition: Plain-English mental model
02 Visuals: Step-by-step diagrams
03 Practice: Clean examples & tradeoffs

1. Complete Comparison of All 5 SQL JOINs

Join TypeKeeps 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 JOINCartesian 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:

SQL
-- 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!)

SQL
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

Tracing SELF JOIN (employees e LEFT JOIN employees m)
Interactive SQL Execution Tracer · Step 1 of 3 (1. Base Table (employees e))
1SELECT e.name AS employee, COALESCE(m.name, 'None') AS manager
2→FROM employees e
3LEFT JOIN employees m ON e.manager_id = m.emp_id;
Live Table State · 1. Base Table (employees e)3 rows
emp_idnamemanager_id
1PriyaNULL
2Aarav1
3Meera2
What the DB engine just did: Priya is the CEO (manager_id is NULL). Aarav reports to 1 (Priya). Meera reports to 2 (Aarav).

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:

SQL
-- 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 JOIN syntax! You emulate it cleanly with LEFT 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:

SQL
-- 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 ON on two 100,000-row tables produces 10,000,000,000 (10 billion) rows and can crash your database! Always check your row counts before running a CROSS JOIN.


5. Chaining 3+ Tables Cleanly

SQL
-- 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

Q1.Table A has 50 rows and Table B has 20 rows. How many rows does SELECT * FROM A CROSS JOIN B; return?
Q2.Why should you use LEFT JOIN instead of INNER JOIN when doing a SELF JOIN to list every employee and their manager?

Keep Exploring: Related Lessons & Tools

All Courses →