SQL joins explained visually
Switch between INNER, LEFT, RIGHT, and FULL JOIN to see which album and sale rows appear in the result.
Join explorer
Match albums.id to sales.album_id.
Albums · left side
| id | title | Result |
|---|---|---|
| 1 | Night Bus | Matched |
| 2 | Static Bloom | Omitted |
| 3 | Soft Circuit | Omitted |
Sales · right side
| id | album_id | quantity | Result |
|---|---|---|---|
| 101 | 1 | 2 | Matched |
| 103 | 1 | 3 | Matched |
| 104 | 4 | 2 | Omitted |
INNER JOIN result
Keeps only pairs with matching IDs. 2 rows.
| album_id | title | sale_id | quantity |
|---|---|---|---|
| 1 | Night Bus | 101 | 2 |
| 1 | Night Bus | 103 | 3 |
Show the SQL
WITH album_sample AS (
SELECT id, title FROM albums WHERE id IN (1, 2, 3)
), sales_sample AS (
SELECT id, album_id, quantity FROM sales WHERE id IN (101, 103, 104)
)
SELECT a.id AS album_id, a.title, s.id AS sale_id, s.quantity
FROM album_sample AS a
INNER JOIN sales_sample AS s ON a.id = s.album_id
ORDER BY a.id NULLS LAST, s.id;Why Night Bus appears twice
Album 1 matches sales 101 and 103. A join returns one row for each matching pair, so Night Bus appears on two rows. If you need one row per album, summarize sales by album ID before joining or group the result afterward.
Why sale 104 has no match here
The two input tables show small subsets of the record-shop data. Sale 104 refers to album 4, which is outside this album sample. RIGHT and FULL JOIN keep that sale and fill the missing album columns with NULL.
Read the NULLs from the missing side
Static Bloom and Soft Circuit have no matching sale in this sample. LEFT and FULL JOIN keep them, with NULL in the sale columns. A NULL quantity here means that the join found no sales row for that album.
