GROUP BY aggregates rows that share the same values in specified columns so you can run summary calculations like COUNT, SUM, AVG, MAX, and MIN.
GROUP BY clusters rows with identical values in one or more columns, allowing aggregate functions to return a single result per group instead of per row.
Use GROUP BY whenever you need totals, averages, or other aggregates per customer, date, product, or any dimension rather than over the entire table.
Start with SELECT, include aggregate and non-aggregate columns, add FROM and optional JOINs, then finish with GROUP BY listing every non-aggregated column.
Yes—use HAVING after GROUP BY to keep or discard groups based on aggregate results, such as only showing customers with more than five orders.
SQL Server enforces determinism; every selected value must be derived unambiguously.GROUP BY columns tell the engine how to pick one value per group.
This query joins Customers and Orders, then groups by customer to calculate lifetime spend.
SELECT c.id, c.name, SUM(o.total_amount) AS lifetime_value
FROM Customers c
JOIN Orders o ON o.customer_id = c.id
GROUP BY c.id, c.name;
SELECT p.id, p.name, SUM(oi.quantity) AS units_sold
FROM OrderItems oi
JOIN Orders o ON o.id = oi.order_id
JOIN Products p ON p.id = oi.product_id
WHERE o.order_date >= '2024-01-01'
GROUP BY p.id, p.name
ORDER BY units_sold DESC;
Index frequently grouped columns, avoid GROUP BY *expressions* that prevent index use, and push non-aggregate filters into WHERE instead of HAVING.
Aggregate on indexed integers rather than strings, group on surrogate keys, and keep SELECT list minimal to reduce shuffle cost.
.
No. SQL Server evaluates GROUP BY before SELECT aliases are created. Repeat the full expression or use a subquery/CTE.
Up to 32 columns in SQL Server. Be cautious—each extra column multiplies the number of groups and can slow the query.
Not guaranteed. Use ORDER BY explicitly if you need sorted output.