Lesson 1 of 4

Progress: 0/4 exercises solved (0%)

Solve all exercises below to unlock the next lesson.

COUNT, SUM, AVG, MIN, MAX

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 — COUNT, SUM, AVG, MIN, MAX

Aggregate functions collapse multiple rows into a single value.

The five core aggregates

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;

COUNT(*) vs COUNT(column)

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

NULLs in aggregates

SUM, AVG, MIN, MAX all ignore NULL values. If all rows are NULL, the result is NULL (not zero).

Using WHERE with aggregates

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;

Tips

  • Without GROUP BY, aggregate functions condense the entire table into one row.
  • You cannot mix aggregate and non-aggregate columns in SELECT without GROUP BY (or a window function).

Learning context

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

  • Separate dimensions from measures
  • Group only by columns needed in final grain
  • Use HAVING only for aggregated filters

Common mistakes

  • Mixing row-level and aggregated logic
  • Grouping by too many columns
  • Using WHERE for aggregated conditions

High-level strategy

  • Draft base rows first
  • Add GROUP BY and verify expected number of groups
  • Add HAVING and ordering last

Task ladder

  1. Baseline query: get a correct first output.
  2. Add one complication: edge case, join, filter, or ranking rule.
  3. Re-run and refine until output is stable and explainable.

Transparent data checks

  • Check row count before and after each major clause.
  • Validate one manual sample row for correctness.
  • Confirm final ordering and column naming for trust.

Retention loop

  • Run the solution once.
  • Rewrite from memory with one variation.
  • Explain the logic in plain English in one sentence.

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

Quick check: COUNT, SUM, AVG, MIN, MAX

3 quick questions. One at a time. Instant score at the end.

Exercise 1

Total number of employees

Enterprise Workforce Analyticseasy

Return a single column named total with the total number of employees.

Run a query to see results here.
Exercise 2

Average product price

Commerce Operations Intelligenceeasy

From the store sandbox, return the average product price as a column called avg_price.

Run a query to see results here.
Exercise 3

Highest and lowest salary

Enterprise Workforce Analyticseasy

Return max_salary and min_salary for all employees (using MAX and MIN).

Run a query to see results here.
Exercise 4

Total revenue from all order items

Commerce Operations Intelligenceeasy

From the store sandbox, return the total revenue as total_revenue — the sum of quantity * unit_price across all order items.

Run a query to see results here.

Lesson 1 of 4

0/4 solved (0%)

Finish this lesson to unlock next.