GROUP BY can contain multiple columns. SQL creates groups based on the unique combination of values from all specified grouping columns.
Business reports often need more detailed summaries. For example, total sales may need to be calculated separately for every region and product category.
If a company wants total sales by region and product category, both Region and Category can be included in GROUP BY.
SELECT
Region,
Category,
SUM(SalesAmount) AS TotalSales
FROM Sales
GROUP BY Region, Category;
Multiple-column grouping is useful for regional sales analysis, department and designation reports, customer segmentation, product analysis, and detailed financial reporting.
- GROUP BY can contain multiple columns.
- Rows are grouped by the combination of values.
- Multiple grouping columns create more detailed summaries.
- Aggregate functions can be used with multiple groups.
- The order of grouping columns affects how the result is organized.
Can GROUP BY contain multiple columns?
Yes. Multiple columns can be used to create groups based on their combined values.
Why use multiple columns in GROUP BY?
It allows analysts to create more detailed summaries, such as sales by region and category.