← SQL and databases

Link tables with keys

Store another table's id in a column, so a row can point at exactly one row elsewhere.

A review says 'Great chorus'. Which song? The review stores a number, and the number points at one row.

The reviews table does not store song titles. It stores a song_id: the id of a row in songs. A column that holds another table's key is called a foreign key. A join matches it up: reviews.song_id = songs.id.

Why not just store the title? Titles can change or repeat. An id never does.

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, reviews.stars
FROM reviews
JOIN songs ON reviews.song_id = songs.id;
The reviews table has 10 rows, and every song_id matches a song. What will this show?

RunRun it and compare the result with your prediction.

Run

Run it. Open Tables in this database to see the song_id column.

Part What it does
reviews.song_id The id of the song this review is about
songs.id The primary key of the songs table
ON reviews.song_id = songs.id Pairs each review with its song
lesson.sql
SELECT songs.title, reviews.stars
FROM reviews
JOIN songs ON reviews.song_id = songs.id;
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, AVG(reviews.stars)
FROM reviews
JOIN songs ON reviews.song_id = songs.id
WHERE songs.title = 'Glass Hearts';

Join, filter to one song, average the stars.

What do the reviews actually store?

Run the query. What is the average rating of the song Glass Hearts?

A song's title is corrected in the songs table. What happens to its reviews?

ModifyChange the code on purpose and check the result.

Modify

Change the query so it shows each song title with the review's comment.

Try it yourselfYour edits are saved in this browser.
Build up to it
In reviews.song_id = songs.id, what is songs.id?

Need a hint?

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

Swap reviews.stars for the column with the words.

Another hint

The column is reviews.comment.

Show each review's comment beside its song title.

    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 the average rating of each reviewed song, with the best first.

    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, group by the song's title, and average the stars.

    Another hint

    Add ORDER BY AVG(reviews.stars) DESC for best first.

    Show each reviewed song with its average stars, best first.

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

      Now you can
      Next lesson11. Ask the database from PythonRun a SQL query from a Python program, loop over the rows it returns, and pass values in safely with ?.
      For teachers

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

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

      Students will be able to

      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.

      The hard part (on purpose)

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

      When students get stuck

      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.

      Where it leads

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

      What each step is for