Sub topic 1
Quick recap: how a join matches rows
A join combines rows from two tables. It compares values in related columns and places matching rows in one result. The ON condition defines what counts as a match.
This chapter assumes you know basic SELECT, WHERE, and ORDER BY statements, primary and foreign keys, and the difference between INNER JOIN and LEFT JOIN.
Prerequisites
You should also understand NULL, which represents a missing or unknown value, and be familiar with GROUP BY and aggregate functions such as SUM.
A simple matching join
Suppose an employees table stores each employee's department ID. A departments table stores the department name.
CREATE TABLE departments (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
department_id INT REFERENCES departments(id)
);
INSERT INTO departments (id, name) VALUES
(10, 'Engineering'),
(20, 'Sales');
INSERT INTO employees (id, name, department_id) VALUES
(1, 'Ava', 10),
(2, 'Ben', 20),
(3, 'Chen', NULL);
SELECT e.name AS employee, d.name AS department
FROM employees AS e
INNER JOIN departments AS d
ON e.department_id = d.id
ORDER BY e.id;The join compares employees.department_id with departments.id. Ava matches Engineering, and Ben matches Sales. Chen has no department ID, so the INNER JOIN excludes Chen.
The aliases e and d are short names for the two tables. A qualified column name, such as e.name, identifies both the table and the column. Qualifying columns prevents ambiguity when both tables contain a column with the same name.
INNER JOIN and LEFT JOIN
An INNER JOIN returns only rows with a match on both sides. Use it when unmatched rows should disappear.
A LEFT JOIN keeps every row from the table on the left. If no matching row exists on the right, the right-side columns contain NULL.
SELECT e.name AS employee, d.name AS department
FROM employees AS e
LEFT JOIN departments AS d
ON e.department_id = d.id
ORDER BY e.id;This result includes Chen with a NULL department:
| employee | department |
|---|---|
| Ava | Engineering |
| Ben | Sales |
| Chen | NULL |
A common error is placing a condition on the right table in WHERE. That can remove the NULL rows and make a LEFT JOIN behave like an inner join.
SELECT e.name, d.name
FROM employees AS e
LEFT JOIN departments AS d
ON e.department_id = d.id
WHERE d.name = 'Engineering';This query keeps only employees whose matched department is Engineering. Chen is removed because NULL = 'Engineering' is not true.
To keep all employees while limiting which department rows can match, put the condition in ON:
SELECT e.name, d.name
FROM employees AS e
LEFT JOIN departments AS d
ON e.department_id = d.id
AND d.name = 'Engineering'
ORDER BY e.id;Now Ava has Engineering, while Ben and Chen remain with NULL.
Predicting result size
For an inner join, the result contains one output row for each pair that satisfies the ON condition. A unique primary-key match usually gives each left row zero or one result row. A non-unique match can produce several rows for one left row.
The next chapters use these same ideas when one table appears twice. The table aliases distinguish the two roles, and the ON condition still determines which rows match.
Exercise
Which employees appear in the INNER JOIN example, and why does Chen not appear?
Show answer
Ava and Ben appear because their department_id values match department IDs. Chen does not appear because NULL does not match any department ID.