4.4.8 · D3Databases

Worked examples — Aggregate functions — COUNT, SUM, AVG, MIN, MAX

3,383 words15 min readBack to topic

This page is the drill hall. The parent note taught you the five functions and the golden NULL rule. Here we hit every corner case — the empty table, the all-NULL column, the single row, the mixed group, the exam trap — so no result ever surprises you.


The scenario matrix

Every aggregate query lands in one of these case classes. The right column names the example that covers it.

# Case class What makes it tricky Covered by
C1 All values present, no NULL The "clean" baseline Ex 1
C2 Some NULLs mixed in AVG divides by non-NULL count; MIN/MAX skip NULL Ex 2
C3 A whole column is NULL SUM/AVG → NULL, COUNT → 0 Ex 3
C4 Empty table (zero rows) Degenerate: nothing to fold Ex 4
C5 Single row Limiting case: MIN = MAX = AVG Ex 5
C6 Grouped with an all-NULL group GROUP BY meets the NULL rule Ex 6
C7 DISTINCT inside COUNT/SUM Duplicates removed first Ex 7
C8 Negative + zero values Signs in SUM/MIN/MAX Ex 8
C9 Real-world word problem Translate business question → SQL Ex 9
C10 Exam twist: WHERE vs HAVING vs COALESCE The classic trap combo Ex 10

Two pictures will anchor the whole page. The first shows what an aggregate is — a vertical column of four salary cells (with one NULL cell marked in red) collapsing, along a single arrow, into one summary box holding 600. The red NULL cell reminds you that it is skipped before the fold:

Figure — Aggregate functions — COUNT, SUM, AVG, MIN, MAX

The second shows the two degenerate boundaries side by side, so you can see they give the same answers. Left panel — an all-NULL column of three red NULL cells: COUNT(*) = 3, COUNT(col) = 0, but SUM = NULL and AVG = NULL. Right panel — an empty table (a single dashed box labelled "no rows" in red): COUNT(*) = 0, and SUM/MIN/MAX = NULL. The caption underneath states the rule both panels obey: nothing to add → NULL; nothing to count → 0:

Figure — Aggregate functions — COUNT, SUM, AVG, MIN, MAX

Keep both in view as we work.


Worked examples

We reuse the parent's table where useful, and introduce small tables per case. Read the Forecast and guess the answer yourself before scrolling.

Base table employees (same as parent):

id dept salary
1 A 100
2 A 200
3 B 300
4 B NULL











Recall

Recall Boundary reflex — fill these instantly

SUM over an empty table ::: NULL COUNT(*) over an empty table ::: 0 AVG of an all-NULL column ::: NULL COUNT(col) of an all-NULL column ::: 0 MIN of a single-row table with value 42 ::: 42 MIN/MAX of a column with values 100,200,300 and one NULL ::: 100 and 300 — NULL is skipped, not treated as smallest Does 0 get skipped like NULL in SUM? ::: No — 0 is a real value; only NULL is skipped Does COALESCE(salary,0) change SUM? ::: No (adds 0); but it does change AVG (raises the count) SUM(DISTINCT amount) with amounts 50,50,70 ::: 120 (duplicate 50 removed)


Connections

  • Aggregate functions — COUNT, SUM, AVG, MIN, MAX — the parent concepts these drills exercise.
  • GROUP BY and HAVING — Ex 6 and Ex 10 depend on grouping and group-level filtering.
  • NULL handling in SQL — the skip-NULL rule that shapes Ex 2, 3, 6, 9.
  • COALESCE and NULLIF — the NULL→0 trick in Ex 9 and Ex 10.
  • SQL query execution order — why WHERE can't hold an aggregate (Ex 10).
  • DISTINCT keyword — de-duping inside COUNT/SUM (Ex 7).
  • Window functions — aggregates that keep every row instead of collapsing.

Concept Map

check

optional

Aggregate query

How many rows survive

Zero rows or all NULL

Some non NULL values

COUNT returns 0

SUM AVG MIN MAX return NULL

Functions return real numbers

GROUP BY makes mini tables

HAVING filters groups