0
0
DBMS Theoryknowledge~6 mins

Join algorithms (nested loop, sort-merge, hash join) in DBMS Theory - Full Explanation

Choose your learning style9 modes available
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.
Explanation
Nested Loop Join
This method compares each row from the first table with every row from the second table to find matching pairs. It is simple but can be slow if tables are large because it checks all possible combinations.
Nested loop join checks every pair of rows, making it easy but often slow for big tables.
Sort-Merge Join
Both tables are first sorted by the join key. Then, the algorithm walks through the sorted tables together, matching rows with the same key efficiently. This method is faster than nested loops when sorting is cheap or data is already sorted.
Sort-merge join uses sorting to quickly find matching rows by scanning tables in order.
Hash Join
One table is used to build a hash table based on the join key. Then, the other table is scanned, and for each row, the hash table is checked for matches. This method is very fast when the hash table fits in memory and the join keys are well distributed.
Hash join uses a hash table to quickly find matching rows, making it efficient for large datasets.
Real World Analogy

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).

Nested Loop Join → Checking every name in the first list against every name in the second list one by one.
Sort-Merge Join → Sorting both lists alphabetically and then moving through them side by side to find matching names.
Hash Join → Making a quick lookup table from one list to instantly find if a name from the other list exists.
Diagram
Diagram
┌─────────────────────┐       ┌─────────────────────┐
│     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    │
└───────────────────────────────┘
This diagram shows the flow of three join algorithms starting from two tables and how each method processes them.
Key Facts
Nested Loop JoinA join method that compares each row of one table with every row of another table.
Sort-Merge JoinA join method that sorts both tables by the join key and merges them by scanning in order.
Hash JoinA join method that builds a hash table from one table and probes it with rows from the other table.
Join KeyThe column or set of columns used to match rows between two tables.
Hash TableA data structure that allows fast lookup of values based on keys.
Common Confusions
Believing nested loop join is always inefficient.
Believing nested loop join is always inefficient. Nested loop join can be efficient for small tables or when indexes exist, so it is not always slow.
Thinking sort-merge join requires both tables to be fully sorted beforehand.
Thinking sort-merge join requires both tables to be fully sorted beforehand. Sort-merge join includes the sorting step as part of the process if tables are not already sorted.
Assuming hash join works well regardless of memory size.
Assuming hash join works well regardless of memory size. Hash join performs best when the hash table fits in memory; otherwise, performance can degrade.
Summary
Join algorithms help databases combine rows from two tables based on matching keys.
Nested loop join checks every pair of rows, sort-merge join sorts and merges, and hash join uses a hash table for fast lookup.
Choosing the right join algorithm depends on table size, data order, and available memory.