6 MIN · 2026-08-04

SQL joins for beginners: how tables shake hands

Why tables rarely live alone, and how INNER JOIN and LEFT JOIN turn them into one. With examples from the SQLazo record store.

SQL joins for beginners: how tables shake hands

If you're new to SQL, the syntax is rarely the hard part. The hard part is a stranger idea: your data doesn't live in one big table. A track lives in tracks. The album it belongs to lives in albums. The band that recorded it lives in artists. Three tables for one song.

That's not a design flaw — it's the point. This article explains why tables live apart and how joins bring them back together, using the record store that sits behind every SQLazo exercise.

Why tables live apart

The store's catalog is a chain:

TableRowsWhat it holds
artists12Bands, with name, country, formed_year
albums17Albums, each carrying its artist's id
tracks49Songs, each carrying its album's id

Look at the ids. albums doesn't store the artist's name — it stores artist_id, a number pointing at a row in artists. tracks does the same with album_id. Columns that point like this are called foreign keys.

Why not just repeat the band's name on every album? Because if The Amber Lines ever changed their name, you'd have to fix every album and every track. Stored once in artists, the name lives in exactly one place.

The handshake: INNER JOIN

A join says: take this table, and for every row, find the matching row in that one. Matching means the condition after ON is true:

SELECT t.title, a.title AS album
FROM tracks t
JOIN albums a ON t.album_id = a.id;

49 rows come back — the same count as tracks, because every track has an album.

Notice the aliases: both tables have a column called title, so we have to say which is which. t.title is the track, a.title is the album. And note that JOIN and INNER JOIN are the same thing — INNER just spells out the default.

If a row finds no partner, an INNER JOIN simply doesn't show it. Both sides must exist for the row to appear.

A tiny worked example

Take the Spanish artist Marina Sol. artists says she is id 2. albums has two rows with artist_id = 2 — Mar Adentro and Las Luces. The join pairs them up:

SELECT ar.name, a.title
FROM albums a
JOIN artists ar ON a.artist_id = ar.id
WHERE ar.name = 'Marina Sol';
Marina Sol | Mar Adentro
Marina Sol | Las Luces

That's the whole trick: each album row reached out to the artist row it points at, and the WHERE kept only one artist.

LEFT JOIN: keeping the unmatched

INNER JOIN has a blunt edge: when a row has no partner, it disappears from the answer. Sometimes the missing partner is the news. Thirteen of the store's fifteen customers have a support rep; two — Sara Nilsson and Grace Kim — have none. Their support_rep_id is NULL, so an inner join would drop them silently. Ask instead:

SELECT c.first_name, c.last_name, e.first_name AS rep
FROM customers c
LEFT JOIN employees e ON c.support_rep_id = e.id;

Fifteen rows come back. The two customers without a rep stay, and where the rep's name should be there's a NULL. A LEFT JOIN keeps every row of the left table; the right side fills in whatever it can.

Flip it around: which employees support no customers? Now employees is the left table, and a missing partner is exactly what we're hunting for:

SELECT e.first_name, e.last_name
FROM employees e
LEFT JOIN customers c ON e.id = c.support_rep_id
WHERE c.id IS NULL;

Four people come back — Jane Foster (general manager), Tom Reyes (sales manager), plus Sofia Duarte and Ken Ito. A LEFT JOIN followed by an IS NULL filter is the classic recipe for "things that don't have a match".

Takeaway

Joins are handshakes on shared ids. Use INNER JOIN for "only the matches" and LEFT JOIN for "keep the left side whole, NULL where nothing matched".

Keep exploring: the joins chapter has seven graded exercises on this — including chaining three tables together and joining a table to itself.

Keep learning