A SQL clause that groups rows sharing one or more common column values, enabling aggregate functions (SUM, COUNT, AVG, etc.) to be applied to each group independently.
- GROUP BY is foundational to relational analytics: Virtually every reporting query that summarizes data by category, period, or dimension uses GROUP BY.
- SQL Server processes GROUP BY through Sort + Hash Aggregate or Stream Aggregate operators: Understanding these operators in execution plans helps DBAs tune aggregation performance.
- The HAVING clause filters groups after aggregation: It is analogous to the WHERE clause for rows and is a common source of confusion for query authors.
- GROUPING SETS, ROLLUP, and CUBE extend GROUP BY capabilities: They allow multiple levels of aggregation in a single query and are useful for summary reports.
- A common performance pitfall is using GROUP BY on unsorted, unindexed columns in large tables: This forces expensive sort operations, while adding covering indexes that include GROUP BY columns can improve performance.
- Related terms: Aggregate Query, Window Functions, Execution Plan, Index Optimization.
