Full answer
SQL describes what you want, and the optimizer decides how to get it. For inner joins, the join order is part of the "how", so the planner tries different orders and picks the one with the lowest estimated cost.
Writing the tables in either order gives the same plan in SQLite:
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');
CREATE INDEX idx_emp_dept ON employees(dept_id);EXPLAIN QUERY PLAN
SELECT e.name, d.name FROM employees e JOIN departments d ON d.id = e.dept_id;QUERY PLAN
SCAN e
SEARCH d USING INTEGER PRIMARY KEY (rowid=?)EXPLAIN QUERY PLAN
SELECT e.name, d.name FROM departments d JOIN employees e ON d.id = e.dept_id;QUERY PLAN
SCAN e
SEARCH d USING INTEGER PRIMARY KEY (rowid=?)Both plans scan employees and find each department through its primary key, whatever order the query text used.
Outer joins are different. students LEFT JOIN enrolments and enrolments LEFT JOIN students mean different things, so the planner can only reorder in ways that keep the meaning.
Sometimes you want to override the planner, usually because its row estimates are wrong. SQLite documents CROSS JOIN as a way to force the left table to be the outer loop. Put departments on the left and the plan changes:
EXPLAIN QUERY PLAN
SELECT e.name, d.name FROM departments d CROSS JOIN employees e ON d.id = e.dept_id;QUERY PLAN
SCAN d
SEARCH e USING INDEX idx_emp_dept (dept_id=?)Now departments is scanned and each department's employees are found through the dept_id index. On this data the planner's own choice was fine; forcing an order is only worth it when you have evidence the planner is wrong. Other engines have their own tools (PostgreSQL's join_collapse_limit setting, SQL Server and Oracle query hints). Use them rarely, and only after reading the plan; fixing statistics or adding an index is usually the better answer.
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');
CREATE INDEX idx_emp_dept ON employees(dept_id);
EXPLAIN QUERY PLAN
SELECT e.name, d.name FROM employees e JOIN departments d ON d.id = e.dept_id;
EXPLAIN QUERY PLAN
SELECT e.name, d.name FROM departments d JOIN employees e ON d.id = e.dept_id;
EXPLAIN QUERY PLAN
SELECT e.name, d.name FROM departments d CROSS JOIN employees e ON d.id = e.dept_id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- For inner joins the optimizer chooses the join order
- Swapping tables in the text usually gives the same plan
- Outer joins limit reordering because order changes meaning
- SQLite CROSS JOIN forces the written order
Common mistakes
- Rewriting join order by hand to 'help' the optimizer without reading the plan
- Assuming LEFT JOIN tables can be swapped freely
Follow-ups
- How does the planner estimate the cost of each order?
- What happens to planning time when a query joins twelve tables?
Tips
- Show or describe an EXPLAIN plan rather than relying on rules of thumb