Skip to content

Summarizing with GROUP BY

Collapse rows to one per group, know exactly which rows each aggregate counts, and spot when a summary hides a problem.

Introductory ~8 min8 steps

What runs in CodeSplash

You will be able to

  • Summarize a table with count, avg, median and stddev_samp
  • Group by one or more columns
  • Explain the difference between count(*) and count(column)

Read first

01

One row per group

Read

An aggregate function collapses many rows to one number. GROUP BY does this once per group.

SELECT species,
       count(*) AS n,
       round(avg(body_mass), 1) AS mean_mass
FROM palmer_penguins_sample
GROUP BY species
ORDER BY species;

Every column in SELECT is either in the GROUP BY or inside an aggregate. Anything else has no single value to show for the group, and SQL rejects it.

02

What gets counted?

Predict

Predict first

Compare count(*) and count(sex) on the same rows. What do you expect?

They always matchcount(sex) can be smallercount(*) can be smaller
Reveal

count(sex) can be smaller. count(*) counts rows; count(column) counts rows where that column isn't NULL. On this table, that's 344 rows, 333 with a recorded sex and 342 with a recorded body mass. The mean silently skips missing values in the same way.

03

See it

In the app

Run this and check the prediction.

SELECT count(*) AS rows_total,
       count(sex) AS with_sex,
       count(body_mass) AS with_mass
FROM palmer_penguins_sample;

Now per species. A difference between n and n_mass shows you how many rows the mean really used.

SELECT species,
       count(*) AS n,
       count(body_mass) AS n_mass,
       round(avg(body_mass), 1) AS mean_mass
FROM palmer_penguins_sample
GROUP BY species
ORDER BY species;
04

Spread and middle

Explore

A mean alone hides a lot. Add the median and standard deviation:

SELECT species,
       median(body_mass) AS median_mass,
       round(stddev_samp(body_mass), 1) AS sd_mass
FROM palmer_penguins_sample
GROUP BY species
ORDER BY species;

stddev_samp divides by n−1n-1, the same as R's sd and pandas' .std(). stddev_pop divides by nn.

Now group by two columns. Change the query to GROUP BY species, island, and add island to SELECT. You'll get one row per combination that exists.

05

Write your own summary

In the app

Now write one from a task. It opens in a new SQL tab, and Check compares your result with the answer's.

SQL exercise · penguins

One row per group

For each species, show how many penguins there are and their mean body mass. Call the columns species, n and mean_mass.

Hints
  • GROUP BY species gives one row for each species.
  • avg() skips missing values, so n and the rows behind the mean can differ.
Show a solution
SELECT species, count(*) AS n, avg(body_mass) AS mean_mass
FROM {{table}}
GROUP BY species;

In the app this opens in a real SQL tab on the sample table and checks your result.

06

Check your understanding

Checkpoint

Check yourself

Run the per-species query. What is the mean body mass of Gentoo penguins, in grams?

Show the answer

Answer: 5076

About 5,076 grams, against roughly 3,700 for each of the other species. A summary this far apart is worth testing, which the last step does.

Check yourself

There are 124 Gentoo penguins. How many rows did the Gentoo mean actually use?

Show the answer

Answer: 123

count(body_mass) is 123. One Gentoo has no body mass, and avg skipped it without comment.

Check yourself

Why does SELECT species, island, count(*) FROM palmer_penguins_sample GROUP BY species fail?

Show the answer

Answer: b

Each result row is one species, and a species can live on several islands, so there's no one value to show. Add island to the GROUP BY, or aggregate it.

07

Is the difference real?

Reflect

Means of 3,701 and 5,076 look very different. How would you decide whether a smaller gap, such as 3,701 against 3,733, is more than chance? A GROUP BY can't tell you: it describes these rows and says nothing about what you'd see next time.

That's where SQL hands over to a test:

08

Recap

Recap
  • GROUP BY gives one row per group; every other column must be aggregated.
  • count(*) counts rows, and count(column) skips NULLs. Aggregates skip NULLs too.
  • Summaries describe. Whether a difference is more than noise needs a test.

Concepts in this lesson