SQL Server Interview Question #86

What is GROUP BY?

Aggregate Functions, GROUP BY & Window Functions Mid-Level Intermediate

Detailed Explanation

GROUP BY divides rows into groups based on one or more expressions so aggregate calculations can be performed for each group.

In a grouped query, selected expressions normally must either be part of the GROUP BY or be aggregated. GROUP BY is widely used for reports, dashboards, analytics, and summary queries.

Code Example

SELECT CategoryId,
       COUNT(*) AS ProductCount,
       AVG(Price) AS AveragePrice
FROM dbo.Products
GROUP BY CategoryId;