GROUP BY clusters rows sharing the same column values so aggregate functions return one result per group in Snowflake.
GROUP BY collects rows with identical column values into groups, letting aggregate functions like COUNT()
, SUM()
, and AVG()
return one result per group.
Place non-aggregated columns after GROUP BY
. Each selected column must be in either an aggregate or the GROUP BY list.
SELECT customer_id, SUM(total_amount) AS total_spend
FROM Orders
GROUP BY customer_id;
Add extra columns to the GROUP BY list. Snowflake returns one row for every unique combination.
SELECT customer_id, DATE_TRUNC('month', order_date) AS month, SUM(total_amount) AS revenue
FROM Orders
GROUP BY customer_id, month;
Use HAVING
after GROUP BY to keep or discard whole groups based on aggregate results.
SELECT customer_id, SUM(total_amount) AS total_spend
FROM Orders
GROUP BY customer_id
HAVING total_spend > 1000;
Use these extensions for multi-level totals in one pass. ROLLUP
adds subtotals, CUBE
adds all dimension combinations, and GROUPING SETS
gives full control.
• Limit selected columns to those you need.
• Pre-aggregate heavy datasets in materialized views.
• Use clustering keys on high-cardinality GROUP BY columns for faster scans.
See the next section for fixes.
No. Snowflake doesn’t allow SELECT aliases inside GROUP BY. Reference the original expression or position index.
Yes. You can write GROUP BY 1,2
, but readability suffers. Prefer explicit column names.
Wrap the timestamp in a date function like DATE_TRUNC('month', order_date)
and place the same expression in the GROUP BY list.