Algorithms
SQL Joins
๐ Quick Reference
| Join Type | Returns | Nulls For |
|---|---|---|
| INNER JOIN | Matching rows only | N/A |
| LEFT JOIN | All left + matching right | Non-matching right |
| RIGHT JOIN | All right + matching left | Non-matching left |
| FULL JOIN | All rows from both | Non-matching from either |
| CROSS JOIN | Cartesian product | N/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:
- See INNER JOIN - only matching rows
- Watch LEFT JOIN - all from left, nulls for unmatched right
- Compare with RIGHT JOIN - all from right
- Try FULL JOIN - all rows, nulls on both sides
๐ง Join Implementations
INNER JOIN
SQL(10 lines)CodeLoading 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)CodeLoading 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)CodeLoading 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)CodeLoading 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)CodeLoading 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
| Scenario | Complexity | Notes |
|---|---|---|
| Nested Loop | O(n ร m) | Simple, for small tables |
| Hash Join | O(n + m) | Build hash table, probe |
| Merge Join | O(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)CodeLoading syntax highlighter...
Counting with Zero
SQL(7 lines)CodeLoading syntax highlighter...
Self Join
SQL(4 lines)CodeLoading syntax highlighter...
Multiple Joins
SQL(10 lines)CodeLoading syntax highlighter...
โ ๏ธ Common Pitfalls
1. Forgetting NULL in LEFT/RIGHT JOIN
SQL(10 lines)CodeLoading syntax highlighter...
2. Accidental Cross Join
SQL(7 lines)CodeLoading syntax highlighter...
3. Ambiguous Columns
SQL(9 lines)CodeLoading syntax highlighter...
4. Joining on Non-Indexed Columns
SQL(10 lines)CodeLoading syntax highlighter...
5. Duplicating Rows Unexpectedly
SQL(11 lines)CodeLoading 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
Previous: Part 18: Garbage Collection
Next: Part 20: Bloom Filter
Visualizer:
SQLJoinVisualizer from @tomaszjarosz/react-visualizers