Aggregation is the process of collapsing many rows into a single summary row using mathematical functions. Without aggregation, every analytical question — total runs in a tournament, average strike rate by country, number of centuries per player — would require retrieving every raw row and computing the answer in application code. SQL aggregation pushes this computation into the database engine, which processes data in bulk, close to storage, orders of magnitude faster than transferring rows to an application and aggregating there.
GROUP BY partitions rows into groups sharing a common value before applying aggregate functions to each group. HAVING filters the resulting groups — it is the WHERE clause for aggregated results, evaluated after aggregation rather than before. Together, GROUP BY and HAVING form the foundation of every reporting query, data quality check, and KPI calculation in analytical engineering. Misunderstanding the interaction between GROUP BY, HAVING, and WHERE is one of the most common sources of incorrect aggregations in production dashboards.
For data engineers, mastering aggregation is non-negotiable. Virtually every data warehouse transformation involves summarising transactional data into dimensional models: total revenue per product per day, unique users per cohort per week, p99 latency per service per hour. All of these are GROUP BY queries. Writing them correctly — with the right grouping keys, the right aggregate functions, and the right NULL handling — determines whether business dashboards show correct numbers or subtly wrong ones.