← SQL and databases

Keep only the rows you want

Filter rows with WHERE, using = for text and > or < for numbers.

Fifteen songs is easy to scan. Fifteen million is not, so we ask the database to do the looking.

Last lesson every row came back. Most questions need only some rows: the pop songs, the long ones, the new ones. WHERE keeps the rows that pass a test.

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 title, genre
FROM songs
WHERE genre = 'pop';
Four of the 15 songs are pop. 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
WHERE genre = 'pop' Keeps only rows where the genre column is pop
'pop' Text goes in single quotes, like a value
genre No quotes: this is a column name
lesson.sql
SELECT title, genre
FROM songs
WHERE genre = 'pop';
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.sql
SELECT title, seconds
FROM songs
WHERE seconds > 250;

The test works on numbers too.

What do quotes tell SQL?

A test has three parts.

Run the query. How many songs are longer than 250 seconds?

Why does WHERE genre = pop (no quotes) give an error?

Which query finds songs released after 2021?

ModifyChange the code on purpose and check the result.

Modify

Change the query so it shows the title of every song by Kito.

Try it yourselfYour edits are saved in this browser.
Build up to it
Which test keeps only the soul songs?

Need a hint?

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

Change the column you test and the value in quotes.

Another hint

You want WHERE artist = 'Kito'.

Show the title of every song by Kito.

    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 a set of songs for a playlist you would make, such as short songs or ones played a lot. Choose the test, write it and run it.

    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

    Pick a column to test: year, seconds or plays work well with > or <.

    Another hint

    SELECT title, plays FROM songs WHERE plays > 5000;

    Write a query with your own WHERE test that finds at least one song.

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

      Now you can
      Next lesson3. Put the rows in orderSort with ORDER BY, reverse with DESC, and keep the top few with LIMIT.
      For teachers

      Students filter rows with WHERE, using text in quotes and numbers with > and <. About 25 minutes.

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

      Students will be able to

      Running the lesson

      1. Predict: the error option catches students who think pop must be a column.
      2. Run: change the genre and run again, then ask what happens with a genre that does not exist (no rows, no error).
      3. Investigate: the quotes question is the main idea. Ask what SQL does with a word that has no quotes.

      The hard part (on purpose)

      Quotes. Ask: is this word a column or a value? Columns have no quotes; text values do.

      When students get stuck

      Stretch and support

      Stretch: Combine two tests (next lesson uses AND) or try year >= 2022.

      Support: Give the WHERE line with the value missing.

      Where it leads

      Next lesson: put the rows in order and show only the top few.

      What each step is for