SQL Challenger
0 of 250 complete

Lessons

0 of 250 complete

Saved in this browser

Lesson 63 of 250

Easy

Join using a shared name

Your task

Return each sale ID and album title by joining on a shared column named album_id.

USING joins on columns that have the same name on both sides. A derived table can expose a renamed key.

Sample data

Choose a table
idtitlegenrepricestock
1Night BusJazz187
2Static BloomIndie224
3Soft CircuitElectronic240
4Blue HourJazz163
5CoastlineIndie198
6Low TideAmbient212

Expected output

idtitle
101Night Bus
102Static Bloom
103Night Bus
104Blue Hour
105Coastline
106Static Bloom
Show a hint

Use a subquery that exposes albums.id AS album_id, then USING (album_id).

Joins and set operations guide

Match rows by a key

A join combines columns horizontally. The condition links sales.album_id to albums.id. If an album has three sale records, a join can return its title three times.

SELECT s.id AS sale_id, a.title, s.quantity
FROM sales AS s
JOIN albums AS a ON a.id = s.album_id;

Preserve rows without a match

A LEFT JOIN retains each album. The quantity condition belongs in ON because it defines which sales qualify as matches. Placing it in WHERE would also remove albums whose sale columns are NULL.

SELECT a.title, s.id AS large_sale_id
FROM albums AS a
LEFT JOIN sales AS s
  ON s.album_id = a.id
 AND s.quantity >= 3;

Combine result sets vertically

UNION stacks compatible result sets and removes duplicate rows. UNION ALL keeps every row. INTERSECT keeps shared rows, and EXCEPT keeps rows unique to the first result. With set operations, both inputs must return the same number of columns.

SELECT title FROM albums WHERE genre = 'Jazz'
UNION
SELECT title FROM albums WHERE price < 20;
SQLiteCtrl / ⌘ + Enter

Press Control+Space for SQL suggestions. Use the arrow keys to select an option and Enter to insert it.

Results

Loading SQLite…