Lesson Description
Reduce many rows to a single value.
Lesson 1 of 4
Progress: 0/4 exercises solved (0%)
Solve all exercises below to unlock the next lesson.
Lesson Description
Reduce many rows to a single value.
Easy Project
Mini project: Produce an executive KPI table with totals and averages per department, sorted by highest business impact.
Aggregate functions collapse multiple rows into a single value.
SELECT
COUNT(*) AS total_employees,
COUNT(manager_id) AS employees_with_manager, -- skips NULLs
SUM(salary) AS total_payroll,
AVG(salary) AS average_salary,
MIN(salary) AS lowest_salary,
MAX(salary) AS highest_salary
FROM employees;
| What it counts | |
|---|---|
COUNT(*) | All rows, including NULLs |
COUNT(col) | Rows where col is not NULL |
COUNT(DISTINCT col) | Unique non-NULL values in col |
SUM, AVG, MIN, MAX all ignore NULL values. If all rows are NULL, the result is NULL (not zero).
WHERE runs before aggregation, so you can filter the rows being aggregated:
-- Average salary only for the IT department
SELECT AVG(salary) AS it_avg
FROM employees
WHERE department_id = 60;
GROUP BY, aggregate functions condense the entire table into one row.SELECT without GROUP BY (or a window function).This lesson is part of Aggregations & Grouping. Focus on the core idea in COUNT, SUM, AVG, MIN, MAX, then validate with deliberate practice.
What to master
Common mistakes
High-level strategy
Task ladder
Transparent data checks
Retention loop
Logical reasoning for commands
GROUP BY
Why: Defines one output row per grouping key.
Memory cue: Group key = output row identity.
HAVING
Why: Filters after aggregates are calculated.
Memory cue: WHERE before groups, HAVING after groups.
COUNT / SUM / AVG
Why: Converts row-level data into business metrics.
Memory cue: Measure only after grain is correct.
Concept check
3 quick questions. One at a time. Instant score at the end.
Return a single column named total with the total number of employees.
From the store sandbox, return the average product price as a column called avg_price.
Return max_salary and min_salary for all employees (using MAX and MIN).
From the store sandbox, return the total revenue as total_revenue — the sum of quantity * unit_price across all order items.
Lesson 1 of 4
0/4 solved (0%)
Finish this lesson to unlock next.