Group-By
Summaries
Learn how to aggregate data by grouping rows with shared values, then apply summary functions to uncover patterns hidden in raw data.
What Is a Group-By Summary?
A group-by summary is a technique that organizes rows of data into groups based on one or more shared column values, then calculates a single summary value for each group. Instead of looking at thousands of individual rows, you see one meaningful number per group.
Why Group-By Matters in Business
The Two Ingredients
The Core SQL Pattern
How Group-By Works Visually
Key Aggregate Functions
| Function | What It Does | Example Use |
|---|---|---|
| SUM() | Adds all values | Total revenue per region |
| COUNT() | Counts rows | Orders per customer |
| AVG() | Arithmetic mean | Average salary per dept |
| MIN() | Smallest value | Cheapest product per category |
| MAX() | Largest value | Top score per class |
WHERE vs HAVING
SQL Execution Order
How to Build a Group-By Query
Example 1: Total Sales by Region
Question: What is the total sales amount for each region?
| region | total_sales |
|---|---|
| East | $48,200 |
| West | $35,100 |
| North | $22,800 |
Example 2: Multiple Aggregates
Question: For each department, what is the employee count and average salary?
| department | num_employees | avg_salary |
|---|---|---|
| Engineering | 42 | $95,400 |
| Marketing | 18 | $72,300 |
| Sales | 31 | $68,100 |
Example 3: Filtering with HAVING
Question: Which product categories have more than \(50{,}000\) in total revenue?
| category | total_rev |
|---|---|
| Electronics | $128,400 |
| Furniture | $67,900 |
Grouping by Multiple Columns
You can group by two or more columns to create finer sub-groups. Each unique combination of values becomes its own group.
| region | product | total |
|---|---|---|
| East | Widget A | $12,400 |
| East | Widget B | $8,600 |
| West | Widget A | $15,200 |
| West | Widget B | $5,900 |
Common Mistakes
WHERE SUM(sales) > 100). WHERE cannot see aggregates; use HAVING.COUNT(*) counts all rows including NULLs. COUNT(col) skips NULL values in that column. Choosing the wrong one skews results.Summary Cheat Sheet
| Concept | Purpose | Key Detail |
|---|---|---|
| GROUP BY | Create groups from rows | Runs after WHERE |
| SUM / AVG | Numeric summaries | Ignore NULLs |
| COUNT(*) | Count all rows | Includes NULLs |
| COUNT(col) | Count non-null values | Skips NULLs |
| HAVING | Filter groups | Runs after GROUP BY |
| Multi-column | Finer sub-groups | Each combo = 1 group |
Question 1 of 5
Which SQL clause is used to filter groups after aggregation?
Question 2 of 5
Given the query:
SELECT dept, AVG(salary) FROM employees GROUP BY dept
How many rows will the result have?
Question 3 of 5
What is wrong with this query?
SELECT region, product, SUM(sales) FROM orders GROUP BY region
Question 4 of 5
A table has 100 rows. The "status" column has 10 rows where the value is NULL. If you GROUP BY status, what will COUNT(status) return for the group corresponding to the NULL values?
Question 5 of 5
In standard SQL execution order, which step runs immediately after GROUP BY?