Algorithms

SQL Joins

๐Ÿ“‹ Quick Reference

Join TypeReturnsNulls For
INNER JOINMatching rows onlyN/A
LEFT JOINAll left + matching rightNon-matching right
RIGHT JOINAll right + matching leftNon-matching left
FULL JOINAll rows from bothNon-matching from either
CROSS JOINCartesian productN/A
One-liner: SQL joins combine rows from multiple tables based on related columns.

๐ŸŽฎ Interactive Visualizer

Watch how different joins combine data from two tables:

Loading visualizer...
Try these operations:
  1. See INNER JOIN - only matching rows
  2. Watch LEFT JOIN - all from left, nulls for unmatched right
  3. Compare with RIGHT JOIN - all from right
  4. Try FULL JOIN - all rows, nulls on both sides

๐Ÿ”ง Join Implementations

INNER JOIN

SQL(10 lines)
Code
Loading syntax highlighter...
employees              departments           Result
โ”Œโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ id โ”‚ name  โ”‚deptโ”‚   โ”‚ id โ”‚ name    โ”‚     โ”‚ e.nameโ”‚ d.name  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”ค   โ”œโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค     โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1  โ”‚ Alice โ”‚ 1  โ”‚   โ”‚ 1  โ”‚ Sales   โ”‚ โ†’   โ”‚ Alice โ”‚ Sales   โ”‚
โ”‚ 2  โ”‚ Bob   โ”‚ 2  โ”‚   โ”‚ 2  โ”‚ IT      โ”‚     โ”‚ Bob   โ”‚ IT      โ”‚
โ”‚ 3  โ”‚ Carol โ”‚ 3  โ”‚   โ”‚ 4  โ”‚ HR      โ”‚     โ”‚ Carol โ”‚ Marketingโ”‚
โ”‚ 4  โ”‚ Dave  โ”‚ 5  โ”‚   โ””โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ””โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”˜                        Dave excluded (dept 5 not found)
                                           HR excluded (no employees)

LEFT JOIN (LEFT OUTER JOIN)

SQL(6 lines)
Code
Loading syntax highlighter...
Result:
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ e.nameโ”‚ d.name  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Alice โ”‚ Sales   โ”‚
โ”‚ Bob   โ”‚ IT      โ”‚
โ”‚ Carol โ”‚ Marketingโ”‚
โ”‚ Dave  โ”‚ NULL    โ”‚  โ† Dave included, no matching dept
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

RIGHT JOIN (RIGHT OUTER JOIN)

SQL(6 lines)
Code
Loading syntax highlighter...
Result:
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ e.nameโ”‚ d.name  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Alice โ”‚ Sales   โ”‚
โ”‚ Bob   โ”‚ IT      โ”‚
โ”‚ Carol โ”‚ Marketingโ”‚
โ”‚ NULL  โ”‚ HR      โ”‚  โ† HR included, no matching employee
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

FULL JOIN (FULL OUTER JOIN)

SQL(6 lines)
Code
Loading syntax highlighter...
Result:
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ e.nameโ”‚ d.name  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Alice โ”‚ Sales   โ”‚
โ”‚ Bob   โ”‚ IT      โ”‚
โ”‚ Carol โ”‚ Marketingโ”‚
โ”‚ Dave  โ”‚ NULL    โ”‚  โ† Unmatched left
โ”‚ NULL  โ”‚ HR      โ”‚  โ† Unmatched right
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

CROSS JOIN

SQL(5 lines)
Code
Loading syntax highlighter...
Result (4 employees ร— 3 departments = 12 rows):
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ e.nameโ”‚ d.name  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Alice โ”‚ Sales   โ”‚
โ”‚ Alice โ”‚ IT      โ”‚
โ”‚ Alice โ”‚ HR      โ”‚
โ”‚ Bob   โ”‚ Sales   โ”‚
โ”‚ Bob   โ”‚ IT      โ”‚
โ”‚ ...   โ”‚ ...     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“Š Join Complexity

ScenarioComplexityNotes
Nested LoopO(n ร— m)Simple, for small tables
Hash JoinO(n + m)Build hash table, probe
Merge JoinO(n log n + m log m)Sort both, merge

The query optimizer chooses based on:

  • Table sizes
  • Available indexes
  • Statistics

โœ… When to Use Each Join

INNER JOIN

  • Default choice when you only want matching data
  • Foreign key relationships that must exist
  • Filtering - exclude rows without matches

LEFT JOIN

  • Optional relationships - show all from main table
  • Finding orphans - WHERE right.id IS NULL
  • Aggregations - include items with zero count

RIGHT JOIN

  • Rarely used - usually rewrite as LEFT JOIN
  • Legacy queries - maintaining existing code
  • Specific ordering - when right table is primary

FULL JOIN

  • Reconciliation - find mismatches between tables
  • Merging data - combine from both sources
  • Finding orphans on both sides

๐Ÿงฉ Common Patterns

Finding Orphan Records

SQL(11 lines)
Code
Loading syntax highlighter...

Counting with Zero

SQL(7 lines)
Code
Loading syntax highlighter...

Self Join

SQL(4 lines)
Code
Loading syntax highlighter...

Multiple Joins

SQL(10 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Pitfalls

1. Forgetting NULL in LEFT/RIGHT JOIN

SQL(10 lines)
Code
Loading syntax highlighter...

2. Accidental Cross Join

SQL(7 lines)
Code
Loading syntax highlighter...

3. Ambiguous Columns

SQL(9 lines)
Code
Loading syntax highlighter...

4. Joining on Non-Indexed Columns

SQL(10 lines)
Code
Loading syntax highlighter...

5. Duplicating Rows Unexpectedly

SQL(11 lines)
Code
Loading syntax highlighter...

๐ŸŽฏ Interview Practice

Test your SQL JOIN knowledge with 10 curated interview questions:

๐ŸŽค SQL Join Interview Mode - Coming Soon


๐ŸŽค Interview Tips

Q: What's the difference between INNER and LEFT JOIN?
"

INNER JOIN returns only rows that match in both tables. LEFT JOIN returns all rows from the left table plus matching rows from the right, with NULL for non-matches.

Q: When would you use FULL OUTER JOIN?
"

To find mismatches between two tables - rows that exist in one but not the other. Useful for data reconciliation, migration validation, or finding orphan records on both sides.

Q: How would you find customers with no orders?
"
Use LEFT JOIN with NULL check:
SELECT c.* FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.id IS NULL
Q: What's the performance difference between joins?
"

INNER JOIN is often fastest (fewer rows). JOIN performance depends on indexes, table sizes, and join algorithm (nested loop, hash, merge). Always index foreign key columns.


๐Ÿ“š Series Navigation


Visualizer: SQLJoinVisualizer from @tomaszjarosz/react-visualizers