← Teacher guide

Join two tables

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

SQL and databases · More than one table · about 30 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 9 min · Investigate 7 min · Modify and Make 14 min

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.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

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

What you will seeA question to askThe fix
ambiguous column nameWhich table does that column belong to?Put the table name and a dot in front of it.
Far too many rows.Does the join have an ON line?Add ON so each song is only paired with its own artist.
A song is missing from the result.Does its artist appear in the artists table, spelled the same?A join only keeps rows that match. Check the spelling in both tables.

Answers to the checks

  1. Predict: There are 15 songs, and every artist is in the artists table. What will this show?
    15 rows. For each song, JOIN finds the artists row where the names match, and puts the two together.
  2. Count the Australian songs: Run the query. How many songs are by artists from Australia?
    4
  3. Read the matching rule: What does ON songs.artist = artists.name do?
    Pairs each song with the artist row whose name matches
    ON is the matching rule. Each song is paired with the artist row where the two names are equal.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

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

Make

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

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.

Exit ticket

Why keep artists in their own table instead of a country column in songs?

A country is stored once for each artist, so a mistake is fixed in one place and nothing is repeated.

What each step is for

Where it leads

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