Using GROUP BY
on multiple columns in SQL allows you to group rows based on combinations of values in those columns. This is useful when you need to analyze data at a more granular level, considering multiple dimensions together.
General Syntax:
Key Points:
- Columns in
GROUP BY
:- Include all the columns you want to group by.
- The query groups rows that have the same values in all specified columns.
- Aggregate Functions:
- Use aggregate functions like
COUNT()
,SUM()
,AVG()
, etc., to compute summaries for each group.
- Use aggregate functions like
- Order of Columns:
- The order of columns in the
GROUP BY
clause matters if you later useORDER BY
without specifying a custom order.
- The order of columns in the
Example Use Case:
Imagine a table sales
with the following columns: region
, product
, and revenue
.
Query:
To find the total revenue for each combination of region
and product
:
How It Works:
- The query groups rows by unique combinations of
region
andproduct
. - For each group, it calculates the
SUM(revenue)
.
Result:
region | product | total_revenue |
---|---|---|
North | Widget A | 1000 |
North | Widget B | 1500 |
South | Widget A | 800 |
South | Widget C | 2000 |
Notes:
- All non-aggregate columns in the
SELECT
statement must be included in theGROUP BY
clause. - If you add more columns to the
GROUP BY
clause, it further subdivides the groups.
By combining multiple columns in GROUP BY
, you can analyze data with more complex relationships.