← Teacher guide

Link tables with keys

Students link two tables with a foreign key, join on it, and see why tables link by id rather than by name.

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 8 min · Investigate 8 min · Modify and Make 14 min

Running the lesson

  1. Predict: the 15-rows option is a good slip. A join gives one row per matching pair, and songs with no reviews do not appear.
  2. Run: compare with the join in lesson 6, which matched on a name. Ask what would break if two artists shared a name.
  3. Investigate: the title question is the main idea of the course. A fact stored once is fixed once.
  4. Make: seven songs have reviews, so seven rows.

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)

Seeing the link. Draw an arrow from each review row to its song row.

When students get stuck

What you will seeA question to askThe fix
ambiguous column name: idBoth tables have an id. Which one do you mean?Write songs.id or reviews.id.
Fewer rows than expected.Which songs have no reviews?A join keeps only rows that have a match.

Answers to the checks

  1. Predict: The reviews table has 10 rows, and every song_id matches a song. What will this show?
    10 rows. Each review is paired with the song whose id equals its song_id.
  2. Find an average: Run the query. What is the average rating of the song Glass Hearts?
    4.5
  3. Changing a title: A song's title is corrected in the songs table. What happens to its reviews?
    Nothing breaks: they use its id
    They point at the song's id, which did not change. The link is the id, and the id stays the same. That is why tables link by key.

Solutions

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

Modify

SELECT songs.title, reviews.comment
FROM reviews
JOIN songs ON reviews.song_id = songs.id;

Make

SELECT songs.title, AVG(reviews.stars)
FROM reviews
JOIN songs ON reviews.song_id = songs.id
GROUP BY songs.title
ORDER BY AVG(reviews.stars) DESC;

Stretch and support

Stretch: Insert a review for a song, then run the join again to see it appear.

Support: Give the join and ask for the grouping.

Exit ticket

Why does reviews store song_id instead of the song's title?

An id is never repeated or changed, so each review points at exactly one song and a title can be fixed in one place.

What each step is for

Where it leads

Next: SQL from Python, which runs the same queries from a program.