Introduction
When databases combine data from two tables, they need a way to match related rows efficiently. Different methods, called join algorithms, solve this problem by finding pairs of rows that fit together based on a condition.
Imagine you have two lists of people: one with names and phone numbers, and another with names and addresses. To find people who appear on both lists, you can either check every name against every other (nested loop), sort both lists alphabetically and then walk through them together (sort-merge), or create a quick lookup table from one list to find matches fast (hash join).
┌─────────────────────┐ ┌─────────────────────┐ │ Table A │ │ Table B │ │ (rows with keys) │ │ (rows with keys) │ └─────────┬───────────┘ └─────────┬───────────┘ │ │ │ │ │ │ │ │ ▼ ▼ ┌───────────────────────────────┐ │ Nested Loop Join │ │ Compare each row of A with B │ └───────────────────────────────┘ │ ▼ ┌───────────────────────────────┐ │ Sort-Merge Join │ │ Sort both tables, then merge │ └───────────────────────────────┘ │ ▼ ┌───────────────────────────────┐ │ Hash Join │ │ Build hash from one table, │ │ probe with other table rows │ └───────────────────────────────┘