Aggregations & Group Filtering
Aggregate datasets using COUNT, SUM, AVG, and filter consolidated data groups using HAVING.
SQL Query Architecture & Optimization Guide
SQL Aggregations summarize multiple rows into single statistical metrics per partition. By combining aggregate functions like COUNT, SUM, AVG, MIN, and MAX with GROUP BY and HAVING clauses, data engineers compute key performance indicators and dataset metrics efficiently.
The SQL engine filters raw rows via WHERE, partitions remaining rows into discrete buckets matching unique GROUP BY keys, calculates aggregate values per bucket, and filters the computed groups using HAVING before returning final rows in SELECT.
Never filter aggregate values in a WHERE clause (e.g. WHERE COUNT(*) > 5 is invalid). Aggregates do not exist yet when WHERE runs. Always use HAVING for aggregate filters. Also, every non-aggregated column in the SELECT list must appear in the GROUP BY clause.
Interactive WebAssembly SQL Playground
Frequently Asked Questions on Aggregations & Group Filtering
What is the exact difference between WHERE and HAVING in SQL?
WHERE filters individual candidate rows before grouping and aggregation occur. HAVING filters grouped metric buckets after GROUP BY calculations have been completed.
How does COUNT(*) differ from COUNT(column_name)?
COUNT(*) counts every row in the dataset, including rows containing NULL values. In contrast, COUNT(column_name) ignores NULL entries and tallies only rows where the specified column contains non-NULL data.
How does GROUP BY treat NULL values across rows?
SQL engines group all NULL values together into a single distinct group partition, rather than treating each NULL as an independent distinct value.