×

sql · level 2 · unit 1 · lesson 1

  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 one answer for every .

So far, COUNT(*) has squashed a whole table into one number. Often you want one number for each kind of thing instead: how many cats, how many dogs, how many fish.

GROUP BY species sorts the rows into piles, one group for each species. The aggregate then runs once inside every group, so you get one row back per group.

Put the grouping column in the SELECT too. Without it you get the numbers, but no way to tell which group each one belongs to.

SELECT species, COUNT(*) AS pets
FROM pets
GROUP BY species;

SQLite tends to hand groups back from A to Z, but only ORDER BY promises an order.