Is it possible to GROUP BY multiple columns using MySQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Grouping results is a fundamental operation in SQL, allowing you to aggregate data meaningfully. In MySQL, one often uses the GROUP BY clause to aggregate data based on one or more columns. This article explores the capability of MySQL to perform grouping on multiple columns, technical explanations of how it works, and some practical examples.
Understanding GROUP BY in SQL
The GROUP BY clause aggregates the results set based on one or more columns. When you use GROUP BY, MySQL combines rows that have identical values in the specified columns into summary rows, like "grouped" rows. Commonly, it's used alongside aggregate functions like COUNT(), SUM(), AVG(), MIN(), or MAX() to perform computations on each group.
Syntax of GROUP BY
The basic syntax of using GROUP BY in SQL is:
When you list multiple columns in the GROUP BY clause, MySQL groups rows into subgroups based on the distinct combinations of the columns.
Grouping by Multiple Columns
MySQL supports grouping by more than one column. This feature is beneficial when you need to perform more fine-grained data grouping. For example, you might have sales data and want to summarize sales figures by each region and product category.
Example Use Case
Consider the following sales table representing a simplified sales record:
| sale_id | region | category | amount |
| 1 | East | A | 100 |
| 2 | West | A | 150 |
| 3 | East | B | 200 |
| 4 | East | A | 120 |
| 5 | West | B | 250 |
Suppose you want to find the total sales amount for each combination of region and category. With multiple columns in the GROUP BY clause, the query will look like this:
The result of this query would be:
| region | category | total_sales |
| East | A | 220 |
| East | B | 200 |
| West | A | 150 |
| West | B | 250 |
The GROUP BY clause produces unique combinations of region and category, calculates the sum of amount for each group, and outputs these aggregates.
Advanced Topics in Grouping
1. NULL Values in Grouping
When grouping by multiple columns, if any column contains NULL values, they are considered as a distinct value. For instance, NULL will not be treated as equal to other groups that contain non-null values.
2. Dynamic Aggregation with Rollup
To generate subtotals and grand totals in addition to regular aggregates, MySQL provides the WITH ROLLUP modifier:
This query will include additional summary rows:
| region | category | total_sales |
| East | A | 220 |
| East | B | 200 |
| East | NULL | 420 |
| West | A | 150 |
| West | B | 250 |
| West | NULL | 400 |
| NULL | NULL | 820 |
The NULL entries in the region or category indicate subtotal rows for each unique value of the other column, and the final NULL row is the grand total.
3. Performance Considerations
- Indexes: Using indexes on grouping columns can enhance performance, especially on large datasets.
- Sort and Group by: Remember that sorting can incur additional costs if not managed well with indexed columns.
Key Points Summary
| Feature | Description |
| Grouping Multiple Columns | Aggregate data based on combinations of multiple column values. |
| Handling NULL Values | NULLs are treated as distinct values during grouping. |
| Rollup | Use ROLLUP to calculate subtotals and totals dynamically. |
| Performance Optimization | Indexes can improve performance during complex groupings. |
MySQL's ability to group by multiple columns adds significant power and flexibility to your data analysis needs by allowing you to create multi-dimensional summaries and insights from your datasets.
Understanding and leveraging these capabilities can greatly enhance your data processing and reporting tasks, making GROUP BY an essential feature in SQL database management and analytics.
Related reading
- Is it possible to handle exceptions within LINQ queries?
- Is it possible to have separate binary logs per database in mysql?
- Is it possible to install only mysqldump on macOS
- Is it possible to install the replication when sql server is running?
- Is it possible to migrate back from Amazon Aurora to native MySQL in Amazon RDS?
- Is it possible to model one-to-many mappings in Yugabyte YCQL?
- Is it possible to monitor transactional replication using Transact SQL
- Is it possible to ORDER results with query or scan in DynamoDB?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.