GROUP BY & Aggregation
GROUP BY & Aggregation — Understand grouping, aggregation functions (COUNT, SUM, AVG, MIN, MAX), and the HAVING clause. The guide walks through GROUP BY Basics, Aggregate Functions, HAVING Clause, ROLLUP & CUBE. GROUP BY groups rows with the same values in specified columns. Each group produces one output row. All non-aggregated columns in SELECT must appear in GROUP BY. Example: SELECT department, COUNT(*) FROM employees GROUP BY department. COUNT(*) counts rows. COUNT(column) counts non-NULL values. SUM adds numeric values. AVG calculates the mean. MIN and MAX find extremes. Aggregate functions ignore NULLs (except COUNT(*)). Use DISTINCT inside aggregates: COUNT(DISTINCT column). The guide is organized into 4 sections that build on each other, each pairing a prose explanation with real SQL you can run as-is. Related guides: SQL Introduction, SELECT Statement, WHERE Clause & Operators, SQL JOINs, Subqueries & CTEs. This is one of 25 SQL guides on SQL Reference — see the full list on the guides page.