Hone

Lessons · SQL · JOIN

Rows from two tables, matched up

JOIN combines rows from two tables where a condition holds, usually one table's foreign key equals the other's id.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Normalised data keeps names in one table and events in another. Reports need both.

How to think about it

Which column in each table matches? Name both tables, give each an alias, and write ON with the matching columns. Then select from either side.

Worked example

SELECT f.title, d.name
One column from each table.
FROM films f
The first table, aliased f.
JOIN directors d ON d.id = f.director_id;
The matching rule.

Your turn

Join films to their studio.

FROM films f JOIN studios s ON s.id = f.;

The trap

Forgetting ON, or writing an ON that always holds: every row pairs with every row, and the result explodes.

Practise JOIN on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.