SQL join exercises with answers
Three join exercises worked end to end — tracks and albums, employees who manage nobody, and a three-table join — with the SQL and the reasoning.
SQL join exercises with answers
Joins are the point where SQL stops being a spreadsheet and starts being a database. The syntax is small — one JOIN ... ON — but the mental model takes a few reps.
Here are three exercises, worked end to end on the SQLazo record store. Try each one before reading the query; the answer is more useful when you have already made a guess.
Exercise 1: track titles next to album titles
The task. List five tracks with the title of the album they belong to. Two columns: track and album.
SELECT t.title AS track, a.title AS album
FROM tracks t
JOIN albums a ON a.id = t.album_id
ORDER BY a.title, t.title
LIMIT 5;
tracks stores album_id, a number that means nothing on its own. The join says: for each track, find the album whose id matches that number, and keep both rows side by side. ORDER BY and LIMIT are only there to keep the output short.
The important line is ON a.id = t.album_id. Swap the sides and it still works; forget it and you get every track paired with every album.
Exercise 2: who manages nobody?
The task. List the employees who do not manage anyone. employees.reports_to points back at the same table.
SELECT e.first_name, e.last_name
FROM employees e
LEFT JOIN employees r ON r.reports_to = e.id
WHERE r.id IS NULL
ORDER BY e.first_name;
This is a self-join: the same table appears twice under two aliases. LEFT JOIN keeps every employee, even the ones with no match; WHERE r.id IS NULL then keeps only the employees whose match is missing — the ones nobody reports to.
That WHERE ... IS NULL trick is the standard way to answer "rows with no children" in one query.
Exercise 3: three tables, one answer
The task. List tracks by Spanish artists, with the track title, album title and artist name.
SELECT t.title AS track, a.title AS album, ar.name AS artist
FROM tracks t
JOIN albums a ON a.id = t.album_id
JOIN artists ar ON ar.id = a.artist_id
WHERE ar.country = 'Spain'
ORDER BY ar.name, t.title;
Each join adds one hop: track → album → artist. The WHERE filters the final rows, after the joins have done their work. This pattern — a chain of joins ending in a filter — covers most real reporting queries.
How to think about any join
- Start from the table whose rows you want.
- Add the table that holds the missing label and write the
ONcondition that connects them (a.id = b.a_id). - Decide whether unmatched rows should survive:
INNER JOINdrops them,LEFT JOINkeeps them. - Add more joins one at a time. If the row count explodes, the
ONcondition is usually wrong.
Keep exploring: the joins chapter has seven graded exercises on this same data, including a three-table join and another self-join.
Questions and answers
What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN keeps only rows that match on both sides. LEFT JOIN keeps every row from the left table, filling the right side with NULL when there is no match.
Can I join a table to itself?
Yes. Give the table two different aliases and join them like any other pair — for example, employees to their managers through reports_to.
Why does a JOIN return more rows than I expected?
Usually because one row on the left matches several rows on the right. Check the ON condition: if it is not the table key, duplicates multiply.