SQL Semantics
Aggregates Skip NULL
SQL aggregates handle NULL differently by function.
Aggregates skip NULL differently
Aggregate functions have sharp edges around NULL. COUNT star counts every row, while column aggregates skip missing values. Note: every number below is read from the compiled aggregate result.
Exact aggregate result
COUNT star is 5, COUNT grade is 4, and COUNT DISTINCT grade is 3. SUM, MIN, and MAX are 330, 70, and 90.
SQL bag/multiset + three-valued NULL logic, deterministic but surprising; tiny finite tables; no engine/perf claims. Set algebra is Book 1.
Duplicates and NULL both matter
For DB rows, COUNT star is 3, COUNT grade is 2, COUNT DISTINCT grade is 1, and SUM grade is 180. Note: the duplicate grade contributes to SUM, while the NULL grade is skipped.
SQL bag/multiset + three-valued NULL logic, deterministic but surprising; tiny finite tables; no engine/perf claims. Set algebra is Book 1.
Summary
COUNT star counts rows, while COUNT column, SUM, MIN, and MAX skip NULL values. Note: bag and NULL semantics are here; set algebra was Book One.