Grouping Data with ROLLUP in SQL
Last Updated :
14 May, 2024
Grouping data is a common operation in SQL when you want to aggregate data based on certain criteria. The MySQL provides the ROLLUP extension to the GROUP BY clause which allows you to generate subtotals and totals for the groups of rows. This article will give an overview of using the ROLLUP extension in MySQL to group data and calculate subtotals and totals.
ROLLUP Extension
The ROLLUP extension augments the GROUP BY clause to create subtotal and grand total rows in the result set. The syntax for using the ROLLUP is as follows:
The ROLLUP extension in MySQL is used with the GROUP BY clause to create subtotals and totals for the groups of the rows. It generates a result set that includes the subtotal rows and total rows.
The syntax for using the ROLLUP is as follows:
SELECT column1, column2, ..., aggregate_function(column)
FROM table_name
GROUP BY column1, column2, ... WITH ROLLUP;
In this syntax:
- column1, column2, ...: Columns used for the grouping the data.
- aggregate_function: An aggregate function such as the SUM(), AVG(), COUNT() etc.
- table_name: The name of the table from which data is retrieved.
Examples of Grouping Data with ROLLUP
Examples 1: Total Sales Calculation
Let's consider a table named sales with the columns region, product, and sales_amount. We want to calculate the total sales amount for each region and product as well as the total sales amount.
Table:
CREATE TABLE sales2 (
region VARCHAR(50),
product VARCHAR(50),
sales_amount INT
);
INSERT INTO sales2 (region, product, sales_amount) VALUES
('East', 'Product A', 5000),
('East', 'Product A', 5000),
('East', 'Product B', 7500),
('East', 'Product B', 7500),
('West', 'Product A', 6000),
('West', 'Product A', 6000),
('West', 'Product B', 6500),
('West', 'Product B', 6500);
The sales2 table now contains the following data:
| region | product | sales_amount |
|--------|-----------|--------------|
| East | Product A | 5000 |
| East | Product A | 5000 |
| East | Product B | 7500 |
| East | Product B | 7500 |
| West | Product A | 6000 |
| West | Product A | 6000 |
| West | Product B | 6500 |
| West | Product B | 6500 |
Query:
SELECT region, product, SUM(sales_amount) AS total_sales
FROM sales2
GROUP BY region, product WITH ROLLUP;
Output:
| region | product | total_sales |
|--------|-----------|-------------|
| East | Product A | 10000 |
| East | Product B | 15000 |
| West | Product A | 12000 |
| West | Product B | 13000 |
| East | NULL | 25000 |
| West | NULL | 25000 |
| NULL | NULL | 50000 |
Explanation: This query selects the total sales amount grouped by region and product from the 'sales2' table. The WITH ROLLUP modifier adds subtotal rows for each region and a total row with NULL values for both region and product.
Example 2: Total Sales by Category
Let's analyze another scenario with the table named orders containing columns category, quantity, and price. We want to determine the total sales amount for each category including the total.
orders1 Table:
CREATE TABLE orders1 (
category VARCHAR(50),
quantity INT,
price INT
);
INSERT INTO orders1 (category, quantity, price) VALUES
('Electronics', 2, 5000),
('Electronics', 3, 7000),
('Clothing', 4, 3000),
('Clothing', 2, 2000);
Query:
SELECT category, SUM(quantity * price) AS total_sales
FROM orders1
GROUP BY category WITH ROLLUP;
Output:
| category | total_sales |
|------------|-------------|
| Electronics| 29000 |
| Clothing | 16000 |
| NULL | 45000 |
Explanation: The query calculates the total sales amount for each category by multiplying the quantity by the price and then summing them up. The WITH ROLLUP modifier adds subtotal rows for each category and a total row with NULL as the category.
Conclusion
The ROLLUP extension in MySQL is a powerful feature for generating subtotals and totals for groups of rows. It simplifies the process of aggregating data and provides valuable insights into the data distribution. By understanding how to use ROLLUP we can efficiently analyze and summarize large datasets in MySQL.
Similar Reads
Grouping Data with ROLLUP in PostgreSQL In database management, reducing and compressing data is one of the most significant jobs. PostgreSQL, which is an open-source, stable relational database management system, boosts many features that are meant to help in this regard. Another element is ROLLUP which maintains the hierarchical data ag
4 min read
SQL COUNT() with GROUP BY Clause The SQL COUNT() function is a powerful tool used to count the number of rows in a dataset. When combined with the GROUP BY clause, it helps group data by specific attributes and count rows within each group. This is particularly useful for summarising data and generating insights.In this article, we
3 min read
GROUPING ID Function in SQL Server SQL Server is a Relational Database Management System that is used to create and manipulate the database. It provides advanced security measures like encryption, access control, and auditing to protect sensitive data from unauthorized access. It Supports a wide range of data types, including structu
6 min read
Grouping Rows in pandas Pandas is the most popular Python library that is used for data analysis. It provides highly optimized performance with back-end source code is purely written in C or Python. Let's see how to group rows in Pandas Dataframe with help of multiple examples. Example 1: For grouping rows in Pandas, we wi
2 min read
What Does GROUP BY 1 Mean in SQL The GROUP BY clause is a powerful tool for aggregating information based on specific columns, making it invaluable for analyzing large datasets. Among its variations, the GROUP BY 1 clause stands out for its simplicity and readability. In this article, we will explain the workings of SQL GROUP BY 1,
3 min read
How to Use GROUP BY and HAVING in SQL? The GROUP BY and HAVING clauses in SQL are two of the most powerful tools available for summarising and analysing data. These clauses enable developers and analysts to efficiently group data, perform aggregate calculations, and filter results based on specific criteria. Whether we're calculating sal
4 min read
How To Get Last Record In Each Group In MySQL? In MySQL, we group the data using the GROUP BY clause. There can be a need to access these groups and get the latest record in each group, for example, tracking the last login timestamp for each user. Knowing how to retrieve the latest record from a group is essential, as it is used in many practica
4 min read
PARTITION BY vs GROUP BY in SQL In SQL both PARTITION BY and GROUP BY are important clauses used for data aggregation and analysis. Sometimes they work as same but they serve different purposes and are applied in different situations. In this article, we'll understand both of them along with the syntax, multiple examples for both
4 min read
MySQL | Group_CONCAT() Function The GROUP_CONCAT() function in MySQL is an aggregation function that combines data from multiple rows into a single string. It is particularly useful for aggregating summaries, such as combining related information into a single field for better readability or reporting. In this article, we will exp
4 min read
DISTINCT vs GROUP BY in SQL SQL (Structured Query Language) is used to manage and manipulate the data in relational databases. It can be used for tasks such as database querying, data editing, database and table creation and deletion, and granting user permissions. We can use the DISTINCT keyword and GROUP BY clause when we wa
4 min read