What is GROUP BY?

GROUP BY splits rows into groups that share a value, so an aggregate function can be calculated separately for each group.

GROUP BY explained

Group by region and SUM(amount) and you get one revenue figure per region. Every selected column must either be in the GROUP BY or be inside an aggregate.

You can group by several columns at once, or by an expression such as the month of a date. HAVING then filters the groups after they are formed.

Example

SELECT region, SUM(amount) FROM orders GROUP BY region returns one row per region with its total.

Frequently asked questions

What is GROUP BY?
GROUP BY splits rows into groups that share a value, so an aggregate function can be calculated separately for each group.
Can you give an example of GROUP BY?
SELECT region, SUM(amount) FROM orders GROUP BY region returns one row per region with its total.