×

sql · level 3 · unit 2 · lesson 4

  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

gives every group a window of its own.

So far each window covered the whole result. splits it into one partition per round, and the window function starts again in each. It is like GROUP BY, except no rows get squashed.

Inside OVER, write PARTITION BY first and ORDER BY second. Here each round gets its own podium, so every round has a first place.

Aggregates work too. AVG(score) OVER (PARTITION BY round) puts each round's average beside every entry in that round.

SELECT round, baker, score,
  RANK() OVER (PARTITION BY round ORDER BY score DESC) AS place
FROM entries
ORDER BY round, place, baker;

Partitions can use several columns, such as PARTITION BY city, day.