datarekha
SQL Easy Asked at AmazonAsked at Walmart

How does GROUP BY behave with multiple columns, and what does each combination represent?

The short answer

GROUP BY on multiple columns creates one group per unique combination of those column values. Each row in the result represents a distinct tuple, and every non-aggregated column in SELECT must appear in the GROUP BY list.

How to think about it

Think of multiple GROUP BY columns as a composite key — the engine forms one group for each unique combination of all the listed values. The more columns you add, the finer-grained the groups become.

Each row = one unique combination

If you group by (country, category, year), a single result row represents all orders from, say, India in Electronics in 2024. If that combination never appears in the data, there is no row for it — the engine does not zero-fill automatically.

The non-aggregated SELECT rule

Every column in SELECT that is not wrapped in an aggregate function must appear in GROUP BY. This is not optional — it is enforced by the SQL standard and by PostgreSQL, BigQuery, Snowflake, and SQL Server.

-- ERROR in PostgreSQL/Snowflake/BigQuery:
SELECT country, city, SUM(revenue)
FROM orders
GROUP BY country;   -- city is unaggregated but not in GROUP BY

The engine cannot produce a deterministic city value for a group that contains multiple cities.

Column order does not matter

GROUP BY a, b and GROUP BY b, a produce identical groups. The order inside GROUP BY is irrelevant — only ORDER BY affects the sort of the output.

Learn it properly Aggregates & GROUP BY

Keep practising

All SQL questions

Explore further

Skip to content