GROUP BY is a SELECT-clause element that partitions result-set rows into logical groups based on one or more expressions. After grouping, aggregate functions like COUNT, SUM, AVG, MIN, and MAX compute a summary value for each group instead of the entire set. Nonaggregated columns in the SELECT list must appear in the GROUP BY list, ensuring deterministic results. GROUP BY executes after WHERE filtering and before HAVING filtering and ORDER BY sorting. Many dialects extend it with ROLLUP, CUBE, and GROUPING SETS for multidimensional analysis.Caveats:- GROUP BY ignores NULL versus NULL equality and treats all NULLs as one group in most engines.- Ordering of groups is not guaranteed unless ORDER BY is specified.- Selecting non-grouped, nonaggregated columns causes error in strict SQL modes (MySQL sql_mode=only_full_group_by, PostgreSQL, SQL Server, Oracle).- Performance depends on indexes or sort/aggregate algorithms; large groupings may require memory or disk spill.
grouping_column_n
(expression) - Column name or expression used to form each groupaggregate_function
(function) - COUNT, SUM, AVG, MIN, MAX, or custom aggregate applied per groupHAVING, ORDER BY, DISTINCT, aggregate functions, GROUPING SETS, ROLLUP, CUBE, window functions
SQL-86
GROUP BY collects rows sharing the same values into groups so aggregate functions compute one result per group.
Yes. The output will mimic DISTINCT by returning one row per unique combination of grouped columns, but no summarization occurs.
WHERE filters rows before grouping. HAVING filters groups after aggregation, typically with aggregate predicates.
Add ORDER BY with the grouping columns or aggregate aliases to sort the final grouped dataset.