Ad space (banner)
🗄️SQL Lessons
Lesson 6 / 50

Aggregating Data with GROUP BY

This lesson covers aggregating data with GROUP BY, so you can compute numbers per group instead of per row. It's for anyone searching "SQL GROUP BY" or "SQL COUNT AVG."

GROUP BY groups rows together, typically paired with aggregate functions like COUNT() or AVG(). It's the go-to technique whenever you want a number "per class" or "per region" — like headcount per class, or total sales per region.

The example groups by class_id and uses COUNT(*) to count students in each class, aliasing the result column as AS count so it reads clearly. Naming the output column like this is a habit worth building early.

A common early mistake is trying to SELECT a column that isn't in the GROUP BY and isn't wrapped in an aggregate function — SQL rejects that, since after grouping, a result can only contain grouped columns or aggregate results. COUNT(*) counts rows, SUM() totals, AVG() averages, and MAX()/MIN() find extremes — this combination shows up constantly in sales dashboards and analytics reports.

SQL
OUTPUT

💡 The SQL engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)