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
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
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';
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?
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.
I can say why text needs quotes and a column name does not.
I can compare a number column with > or <.
Running the lesson
Predict: the error option catches students who think pop must be a column.
Run: change the genre and run again, then ask what happens with a genre that does not exist (no rows, no error).
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
no such column: pop Ask: Is pop a column, or a value in a column? Fix: Put single quotes round the text value.
No rows come back. Ask: How is the genre spelled in the table? Fix: Match the spelling in the data. Text must match letter for letter.
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
Investigate: WHERE is a test applied to each row. Text values go in quotes, numbers do not, and the columns you show can differ from the column you test.