SQL GROUP BY — Group Rows for Aggregation
GROUP BY groups rows by the specified columns so aggregates like COUNT, SUM, or AVG apply per group. Non-aggregate SELECT columns must appear in GROUP BY (strict in PostgreSQL/standard; MySQL depends on ONLY_FULL_GROUP_BY).
Open the interactive DevRef Hub tool →Key facts
| Syntax | SELECT dept, COUNT(*) FROM employees GROUP BY dept; |
|---|---|
| MySQL rollup | GROUP BY dept WITH ROLLUP |
| PostgreSQL rollup | GROUP BY ROLLUP (dept) |
| Rule | Non-aggregate cols must be in GROUP BY |
Frequently asked questions
What does GROUP BY do?
It groups rows by columns so aggregate functions run per group.
Why does GROUP BY error on a column?
Non-aggregate SELECT columns must be in GROUP BY (ONLY_FULL_GROUP_BY in MySQL).
Related pages
SQL GROUP BY is part of DevRef Hub on GOAT Lab — a free reference you can use without signing up, and it works offline. Open the tool above for any custom value.
Part of GOAT Lab — 26 free, browser-based science, engineering and developer tools. No sign-up.