Learn with Abdou

Reading · Lesson 12

One output row per group

Part 1 of 1

Choose the unit of your summary

GROUP BY creates one result group for each distinct grouping value. Grouping by category answers a category-level question; grouping by category and size creates a finer comparison.

Every selected non-aggregate value should describe the group unambiguously. SQLite can accept a bare column outside GROUP BY, but acceptance does not make an arbitrary item name a meaningful label for a category average.

Worked example
SELECT category, COUNT(*) AS items, ROUND(AVG(price), 2) AS average_price FROM menu_items GROUP BY category ORDER BY category;

Each category becomes one group. COUNT counts its menu rows and AVG summarizes their prices; the result is ordered by category so teams can compare it consistently.

The average is per menu item in this fixture, not weighted by purchases. You cannot describe it as the average price customers paid without appropriate order-item data.

Pause and explain. If you add size to GROUP BY, why might the number of result rows increase?