01 / 21

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.

Business Analytics

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

Sales Reports
Total revenue per region, per quarter, or per product line.
Customer Insights
Average order size by customer segment or loyalty tier.
Operations
Count of defective items per factory, per shift, per month.

The Two Ingredients

Grouping Column(s)
The column whose unique values define the groups: Region, Department, Product, Date, etc.
Aggregate Function
The calculation applied within each group: SUM, COUNT, AVG, MIN, MAX, and more.

The Core SQL Pattern

SELECT grouping_column, AGG_FUNCTION(value_column) AS alias FROM table_name GROUP BY grouping_column;

How Group-By Works Visually

Raw Table Region Sales East 200 West 150 East 300 West 250 East 100 West 100 GROUP Grouped East 200, 300, 100 SUM = 600 West 150, 250, 100 SUM = 500 AGG Result Region Total East 600 West 500

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

WHERE
Filters individual rows before grouping. Cannot reference aggregate functions.
HAVING
Filters groups after aggregation. Can reference aggregate results like SUM or AVG.
SELECT region, SUM(sales) AS total FROM orders WHERE year = 2024 -- row filter GROUP BY region HAVING SUM(sales) > 1000; -- group filter

SQL Execution Order

1
FROM: Identify the source table(s).
2
WHERE: Filter individual rows.
3
GROUP BY: Organize remaining rows into groups.
4
HAVING: Filter groups based on aggregate results.
5
SELECT: Compute and return the final columns.
6
ORDER BY: Sort the output.

How to Build a Group-By Query

1
Identify the question: "What do I want to know per group?"
2
Choose grouping column(s): Which column defines the groups? (e.g., Region, Category)
3
Choose the aggregate function: SUM, COUNT, AVG, MIN, or MAX based on your question.
4
Write the query: SELECT grouping column + aggregate, FROM table, GROUP BY grouping column.
5
Add filters: WHERE for row-level, HAVING for group-level.

Example 1: Total Sales by Region

Question: What is the total sales amount for each region?

SELECT region, SUM(amount) AS total_sales FROM orders GROUP BY region;
regiontotal_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?

SELECT department, COUNT(*) AS num_employees, AVG(salary) AS avg_salary FROM employees GROUP BY department;
departmentnum_employeesavg_salary
Engineering42$95,400
Marketing18$72,300
Sales31$68,100

Example 3: Filtering with HAVING

Question: Which product categories have more than \(50{,}000\) in total revenue?

SELECT category, SUM(revenue) AS total_rev FROM products GROUP BY category HAVING SUM(revenue) > 50000;
categorytotal_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.

SELECT region, product, SUM(sales) AS total FROM orders GROUP BY region, product;
regionproducttotal
EastWidget A$12,400
EastWidget B$8,600
WestWidget A$15,200
WestWidget B$5,900

Common Mistakes

✕ Non-Aggregated Column in SELECT
Selecting a column that is neither in GROUP BY nor inside an aggregate function. This causes an error or unpredictable results.
✕ Using WHERE Instead of HAVING
Trying to filter on an aggregate (e.g., WHERE SUM(sales) > 100). WHERE cannot see aggregates; use HAVING.
✕ Forgetting COUNT(*) vs COUNT(col)
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?