×

sql · level 2 · unit 4 · 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

A subquery in SELECT puts the same value on every row.

Each player's points are interesting. Each player's share of all the points is more interesting. For that you need two numbers on every row: the player's total and the grand total.

A subquery can sit in SELECT like a column. (SELECT SUM(points) FROM scores) gives the grand total, 535, on every row.

Multiply by 100 and divide by the grand total for a . Write 100.0, not 100, so the division keeps its decimals, then ROUND the answer.

SELECT player, SUM(points) AS total,
  ROUND(100.0 * SUM(points) / (SELECT SUM(points) FROM scores), 1) AS share
FROM scores
GROUP BY player
ORDER BY total DESC, player;

The subquery ignores the grouping. It always looks at the whole table.