×

sql · level 2 · unit 1 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Every has its place, and the order never changes.

A query is built from clauses: parts that start with a keyword, like WHERE or GROUP BY. They always go in this order: SELECT, FROM, WHERE, GROUP BY, ORDER BY, then LIMIT.

WHERE sieves single rows before any grouping starts, so each group only holds rows that passed. Here only darts scores reach the groups.

ORDER BY works last, on the finished groups. That means it can sort by an aggregate's alias, like ORDER BY total DESC for a leaderboard. A second column after a comma settles ties: ORDER BY total DESC, player.

SELECT player, SUM(points) AS total
FROM scores
WHERE game = 'darts'
GROUP BY player
ORDER BY total DESC;

Put a clause in the wrong place and you get a syntax error near the word that is out of order.