← SQL and databases

Put the rows in order

Sort with ORDER BY, reverse with DESC, and keep the top few with LIMIT.

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
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;
What will this query show?

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.

Order first, then limit.

Without DESC, which song comes first when sorted by seconds?

You want the single newest song. What do you need?

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
Which query shows the shortest song?

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.

      Now you can
      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

      Running the lesson

      1. Predict: the 'first 3 in the table' option shows students who think the table has a built-in order.
      2. Run: delete DESC and run again so the order flips.
      3. 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

      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