Part 6: Mastering the GROUP BY Command
Mastering the GROUP BY Command
How to produce grouped business summaries β total revenue by type, users by country, orders by status β with one output row per category.
Learning Objectives
- Use GROUP BY to organise rows into groups for category-level aggregation
- Apply GROUP BY with COUNT(), SUM(), AVG(), MIN(), and MAX() for grouped business summaries
- Follow the GROUP BY rule: every SELECT column must be aggregated or listed in GROUP BY
- Understand that GROUP BY produces exactly one output row per unique value in the GROUP BY column
GROUP BY organises records into groups and allows aggregate functions β COUNT, SUM, AVG, MIN, MAX β to produce one summary row per group instead of one summary for the entire table. Without GROUP BY, SUM(amount) returns one total for all transactions. With GROUP BY transaction_type, it returns one total per transaction type.
In industry, analysts use GROUP BY to summarize revenue by category, measure customer segments by country, analyze grouped performance by status, build operational reports by classification, and create the business dashboards that show performance across every meaningful business dimension.
Every column in SELECT must either be inside an aggregation function (COUNT, SUM, AVG, MIN, MAX) or listed in the GROUP BY clause. If a column appears in SELECT but not in GROUP BY and not inside an aggregation, the query will produce an error or unpredictable results.
The most common GROUP BY pattern combines it with SUM() to produce revenue totals per category β total by transaction type, total fees by subscription status, total sales by product category. Each unique value in the GROUP BY column becomes one row in the output.
GROUP BY collects all rows with the same value in the specified column into one group. Then the aggregate function calculates its result for each group independently. The final output has exactly one row per unique group value.
GROUP BY unlocks the full power of every aggregate function. GROUP BY with COUNT() measures volume per category. GROUP BY with AVG() measures the typical value per group. GROUP BY with MIN() and MAX() reveals the extreme values within each category. Any aggregate function combined with GROUP BY produces a category-level business summary.
GROUP BY country produces one row per country. GROUP BY category produces one row per product category. GROUP BY order_status produces one row per status. The number of output rows equals the number of distinct values in the GROUP BY column.
GROUP BY is the standard tool for customer segmentation in SQL. Grouping users by country, user_type, or signup period produces the segment-level counts and summaries that marketing, product, and executive teams use for decision-making. The grouped output answers: “how does this metric differ across customer groups?”
Before SQL GROUP BY, analysts exported data to Excel and built pivot tables manually. GROUP BY with COUNT() or SUM() produces the same grouped summary directly from the database β faster, more accurate, and fully reproducible every time the query runs.
Work through each question below. Run your query first, then use Check Answer. A wrong answer reveals the correct SQL with a Copy button; a correct result earns a congratulations message.
Knowledge Check
Five questions β select the best answer for each.
Part 6 Summary
The key ideas from this lecture β keep these in mind as you move forward.
GROUP BY helps analysts summarize business metrics by categories, segments, and operational groups inside organizational datasets. Every SELECT column must be aggregated or in GROUP BY. Output has one row per unique group value.

