Full answer
These are the three main physical ways to execute a join. SQL text does not choose them; the planner does, based on table sizes, indexes and the join condition.
Nested loop. For every row on the outer side, find matching rows on the inner side. With an index on the inner join key, each lookup is a quick index seek, so this is excellent when the outer side has few rows: "these 20 orders and their customers". Without an index, it compares every pair, which is fine for tiny tables and terrible for large ones. It is the only one of the three that handles any join condition, including ranges and inequalities.
Hash join. Read the smaller input and build an in-memory hash table keyed on the join column. Then read the larger input and probe the hash table for each row. Cost grows roughly linearly with the total rows, so it is strong for large joins with =. It needs memory for the hash table and cannot do range conditions.
Merge join. Sort both inputs on the join key (or read them already sorted from indexes), then walk them together like merging two sorted lists. Good when both sides are large and already ordered.
PostgreSQL shows which one it picked. With small tables it chooses a hash join here, and switching methods off for the session shows the alternatives:
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');EXPLAIN (COSTS OFF)
SELECT e.name, d.name FROM employees e JOIN departments d ON d.id = e.dept_id;Hash Join
Hash Cond: (e.dept_id = d.id)
-> Seq Scan on employees e
-> Hash
-> Seq Scan on departments dSET enable_hashjoin = off;
EXPLAIN (COSTS OFF)
SELECT e.name, d.name FROM employees e JOIN departments d ON d.id = e.dept_id;Merge Join
Merge Cond: (d.id = e.dept_id)
-> Index Scan using departments_pkey on departments d
-> Sort
Sort Key: e.dept_id
-> Seq Scan on employees eSET enable_hashjoin = off;
SET enable_mergejoin = off;
EXPLAIN (COSTS OFF)
SELECT e.name, d.name FROM employees e JOIN departments d ON d.id = e.dept_id;Nested Loop
-> Seq Scan on employees e
-> Index Scan using departments_pkey on departments d
Index Cond: (id = e.dept_id)SQLite uses nested loops, helped by indexes and, when no index exists, an automatic temporary index it builds for the query. The enable_* settings are for experiments only; in production you fix statistics, indexes or the query instead.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- Nested loop: per outer row, look up inner matches; great with an index and a small outer side
- Hash join: build a hash on one side, probe with the other; equality only
- Merge join: walk two sorted inputs together
- Only nested loop supports arbitrary conditions such as ranges
- The planner picks the method from sizes, indexes and statistics
Common mistakes
- Saying one algorithm is always best
- Forcing a method with planner settings in production instead of fixing the cause
Follow-ups
- Why can a hash join spill to disk?
- What makes the planner switch from a nested loop to a hash join as data grows?
Tips
- Tie each algorithm to a situation where it wins