← SQL and databases

Join two tables

Combine rows from two tables with JOIN ... ON, matching a column they share.

A song knows its artist's name. It does not know where the artist is from. That lives in a different table.

Real databases keep each kind of thing in its own table, so a fact is stored once. The music library now has two tables: songs and artists. The artist's name is in both. A JOIN lines up each song with its artist by matching that name.

How this lesson works: Predict, Run, Investigate, Modify, Make
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lesson.sql
SELECT songs.title, artists.country
FROM songs
JOIN artists ON songs.artist = artists.name;

Open Tables in this database first to see both tables.

There are 15 songs, and every artist is in the artists table. What will this show?

RunRun it and compare the result with your prediction.

Run

Run it and compare with your prediction.

Part What it does
songs.title The title column of the songs table: table name, a dot, column name
FROM songs JOIN artists Brings the two tables together
ON songs.artist = artists.name Says which columns must match for a song and an artist to be paired
lesson.sql
SELECT songs.title, artists.country
FROM songs
JOIN artists ON songs.artist = artists.name;
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.sql
SELECT songs.title, artists.country
FROM songs
JOIN artists ON songs.artist = artists.name
WHERE artists.country = 'Australia';

A join can be filtered with WHERE like any other query.

Say it aloud: a song and an artist go together when...

Run the query. How many songs are by artists from Australia?

What does ON songs.artist = artists.name do?

ModifyChange the code on purpose and check the result.

Modify

Change the query so it shows each song's title and its artist's country.

Try it yourselfYour edits are saved in this browser.
Build up to it
Which column from the artists table would show where an artist is from?

Need a hint?

Before a hint: what kind of stuck is this? Try a strategy first

Change artists.formed to the column you want.

Another hint

The column is artists.country.

Show each song's title and its artist's country.

    Run your program. This ticks when your code does all of the above.

    MakeBuild something new using what you have learned. Expect a few tries; that is normal.

    Make

    Find out how many songs the library has from each country. Join the tables, then group.

    Try it yourselfYour edits are saved in this browser.
    Need a hint?

    Before a hint: what kind of stuck is this? Try a strategy first

    Join, then GROUP BY the country.

    Another hint

    SELECT artists.country, COUNT(*) FROM songs JOIN artists ON songs.artist = artists.name GROUP BY artists.country;

    Count the songs from each country using JOIN and GROUP BY.

      Run your program. This ticks when your code does all of the above.

      Now you can
      Next lesson7. Add a rowPut a new row into a table with INSERT INTO, then check it with SELECT.
      For teachers

      Students combine two tables with JOIN ... ON and then filter or group the joined rows. About 30 minutes.

      Open the teacher guide: answers, solutions, slides and a worksheet.

      Students will be able to

      Running the lesson

      1. Predict: the 75 rows option is the idea that sinks a join with no ON. Do not reveal why yet; it comes back in the stuck list.
      2. Run: show both tables on the table list first. Ask which column is the same in both.
      3. Investigate: students read ON aloud as a sentence.
      4. Make: the pattern is join first, then GROUP BY or WHERE like any query.

      The hard part (on purpose)

      Picturing what the join does. Use two paper lists and draw lines between the matching rows.

      When students get stuck

      Stretch and support

      Stretch: Show each song with the artist's formed year, newest bands first.

      Support: Give the whole JOIN line and ask students to choose the columns.

      Where it leads

      Next lesson: change the data itself with INSERT, UPDATE and DELETE.

      What each step is for