Every top ten list is the same question: sort everything, then keep the first ten.
A table has no fixed order. ORDER BY sorts the rows, and LIMIT keeps only the first few. Together they make a top list.
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, plays
FROM songs
ORDER BY plays DESC
LIMIT 3;
RunRun it and compare the result with your prediction.
Run
Run it, then delete DESC and run it again to see what changes.
| Part |
What it does |
ORDER BY plays |
Sorts the rows by the plays column, smallest first |
DESC |
Reverses the order: biggest first |
LIMIT 3 |
Keeps only the first three rows |
lesson.sql
SELECT title, plays
FROM songs
ORDER BY plays DESC
LIMIT 3;
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, year
FROM songs
ORDER BY year
LIMIT 3;
Smallest first is the default.
ModifyChange the code on purpose and check the result.
Modify
Change the query so it shows the title and length of the longest song.
Try it yourselfYour edits are saved in this browser.
Build up to it
Need a hint?
Before a hint: what kind of stuck is this? Try a strategy first
The longest song is the biggest value in seconds.
Another hint
ORDER BY seconds DESC LIMIT 1.
Show the title and length of the longest song.
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
Make a top list of your own, such as the three most played rock songs. Use WHERE, ORDER BY and LIMIT together.
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
Combine WHERE, ORDER BY and LIMIT in that order.
Another hint
WHERE comes first, then ORDER BY, then LIMIT.
Make a top list that uses WHERE, ORDER BY and LIMIT.
Run your program. This ticks when your code does all of the above.
Lesson done. You've finished every step you need.
Now you can- Sort rows with ORDER BY and reverse with DESC.
- Keep only the first few rows with LIMIT.
- Combine WHERE, ORDER BY and LIMIT in the right order.
Next lesson4. Count and add upTurn many rows into one answer with COUNT, SUM, AVG and MAX.
For teachers
Students sort with ORDER BY, reverse with DESC, keep the top rows with LIMIT, and combine it with WHERE. About 25 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Students will be able to
- I can sort rows with ORDER BY and reverse with DESC.
- I can keep only the first few rows with LIMIT.
- I can combine WHERE, ORDER BY and LIMIT in the right order.
Running the lesson
- Predict: the 'first 3 in the table' option shows students who think the table has a built-in order.
- Run: delete DESC and run again so the order flips.
- Make: the clause order is fixed. Read the query aloud: which rows, in what order, how many.
The hard part (on purpose)
The fixed order of the parts. Read the query aloud as a sentence.
When students get stuck
- syntax error near ORDER Ask: Which comes first, WHERE or ORDER BY? Fix: The order is SELECT, FROM, WHERE, ORDER BY, LIMIT.
- The wrong end of the list. Ask: Does the order go smallest first or biggest first? Fix: Add DESC for biggest first.
Stretch and support
Stretch: Sort by two columns, such as genre and then plays DESC.
Support: Give the query with ORDER BY done and ask only for LIMIT.
Where it leads
Next lesson: count and add up rows to one answer.
What each step is for
- Investigate: Rows come back in the order you ask for, and LIMIT counts from the top of that order.