Why this lesson matters

Understand count, sum and avg and reason about it with a concrete relational example. Aggregate functions reduce many rows to measures. GROUP BY defines each result group, while HAVING filters those groups after aggregation.

How to reason about it

  • For COUNT, SUM and AVG, the outcome to verify is: Run the statement against sample data and inspect the result before moving to the next case.
  • In COUNT, SUM and AVG, keep this failure controlled: Selecting non-grouped columns beside aggregates or using WHERE when a condition depends on an aggregate leads to invalid or misleading queries.
  • COUNT, SUM and AVG practice target: Calculate order count and revenue by customer, then keep only customers whose order count crosses a chosen threshold.

Practical walkthrough

In the COUNT, SUM and AVG walkthrough: Run the statement against sample data and inspect the result before moving to the next case.

example.sqlsql
SELECT COUNT(*) AS orders, SUM(total) AS revenue, AVG(total) AS avg_order
FROM orders;

Practice it yourself

Practice

COUNT, SUM and AVG exercise

Calculate order count and revenue by customer, then keep only customers whose order count crosses a chosen threshold.

  • Record the expected result before execution
  • Test one valid path and one lesson-specific failure path
  • Explain in two lines which boundary owns the decision

Summary