Data is spread across related tables, and joins stitch it back together. An INNER JOIN — the most common — returns rows that have a match in both tables, based on a key.
Joining two tables
SELECT s.name, p.name AS programmeFROM students sJOIN programmes p ON s.programme_id = p.id; -- match on the key-- only students whose programme_id exists in programmes appear
How it works
The ON clause is the matching condition. For each pair of rows where it's true, the join produces one combined row. Table aliases (s, p) keep the column references short and unambiguous when both tables share a column name.
Key takeaways
- 01An INNER JOIN returns rows with a match in both tables, via the ON key.
- 02The ON clause is the matching condition between the tables.
- 03Use table aliases and qualify columns (s.name) to keep joins clear.
- 04JOIN and INNER JOIN are identical; chain joins for three+ tables.
