Course contentsShow
← Data Science
Lesson 909 of 2,14523. SQL Aggregation and GroupingFree lesson

Combining Multiple Groups with SELECT

Select grouped columns and aggregates together to create meaningful summary reports with hierarchical data.

Combining Multiple Groups with SELECT

What you'll learn: How to select grouped columns alongside aggregate functions to create rich, multi-dimensional summary reports.

When you use GROUP BY with multiple columns, you're not just grouping—you're also free to select those grouping columns in your result set alongside your aggregates. This creates summary tables that show both the category labels and the calculated metrics, making your reports immediately interpretable.

How It Works

Think of it like organizing a sales spreadsheet. If you group by both region and product_category, you can select both of those columns in your SELECT statement along with aggregate functions like SUM(sales) or COUNT(*). The result? A table that shows each unique region-category combination with its corresponding metrics—no guesswork about what the numbers represent.

The Pattern

SELECT 
    region,
    product_category,
    COUNT(*) AS total_orders,
    SUM(revenue) AS total_revenue,
    AVG(order_value) AS avg_order
FROM sales
GROUP BY region, product_category
ORDER BY region, total_revenue DESC;

Here, region and product_category appear in both SELECT and GROUP BY. Each row in the result represents one unique combination of those two columns, with aggregates calculated for that specific group.

Why This Matters

Without selecting your grouping columns, you'd just see numbers with no context—"42 orders, $12,500 revenue" means nothing if you don't know which region and category it represents. By combining grouped columns with aggregates in SELECT, you create self-documenting reports.

Key Takeaway: Always select the columns you're grouping by alongside your aggregates to create meaningful, labeled summary reports—your grouped columns provide the context, and your aggregates provide the insights.