Pull rows out, then filter them

INPUT · Slides

Pull out and filter: round-up

01 / 03

What you can do now

Let us lay out the tools from this chapter.

  • picking columns … SELECT and *
  • narrowing rows … WHERE, the comparisons, LIKE, NOT, IS NULL
  • joining conditions … AND, OR
  • ordering and counting … ORDER BY, LIMIT

Each is short on its own. Using them together is the real thing.

02 / 03

The order is always the same

A query always comes in the same order. When you are stuck, pour what you want into this shape.

SELECT columns FROM table WHERE condition ORDER BY order LIMIT count

You can leave out the parts you do not need, but you cannot shuffle them around.

SELECT name, sold FROM booths  WHERE team = 'Team Moon'  ORDER BY sold DESC  LIMIT 1;

Result

shaved ice | 118

03 / 03

Read a question as four questions

However long the wording, only four things are being asked.

  • which columns to show?
  • which rows to keep?
  • what order?
  • how many?

Jot those down in that order and even a knotty question stops being frightening. Try your hand on the booths table from the school fair.