Yogesh Chauhan's Blog

How to Use ROLLUP Operator in SQL and MySQL?

in SQL/MySQL on January 4, 2020

When we use grouping or aggregate functions, we can use ROLLUP operator to add one or more summary rows into the result set.

ROLLUP operator is available in SQL and MySQL both with slightly different syntax.

ROLLUP Syntax in SQL:


SELECT 
    column_1, column_2, aggregate_function(column_3)
FROM
    table_name
GROUP BY ROLLUP (column_1,column_2);

ROLLUP Syntax in MySQL:


SELECT 
    column_1, column_2, aggregate_function(column_3)
FROM
    table_name
GROUP BY column_1,column_2 WITH ROLLUP;

Let's look at some examples to understand it better.


//Query in MySQL

SELECT seller_id, 
COUNT(*) AS number_of_invoice,
SUM(invoice_total) AS invoice_total
FROM invoices
GROUP BY seller_id WITH ROLLUP;

//Query in SQL

SELECT seller_id, 
COUNT(*) AS number_of_invoice,
SUM(invoice_total) AS invoice_total
FROM invoices
GROUP BY ROLLUP(seller_id);

In this example, we are using ROLLUP with a single column. In invoices are grouped by seller id in the query. Also, we are calculating number of invoice and sum of invoice total for each seller. 

Checkout the DEMO link given at the end of this article.

Look at the last row. That's a summary row. It's added because of ROLLUP operator in Group By. It summarizes all the aggregate columns in the last row.

So, in the result set the number_of_invoice and invoice_total columns are being summarized by the ROLLUP operator.  

Why there is no value in seller_id column in the summarized row?

That's because it can't be summarized. You'll see null value in SQL and empty rows in MySQL.

Let's take a look at another query.


//Query in MySQL

SELECT seller_state, seller_city, COUNT(*) AS total_sellers FROM sellers
WHERE seller_state IN ('FL','IN')
GROUP BY seller_state, seller_city WITH ROLLUP; 

//Query in SQL
​​​​​​​
SELECT seller_state, seller_city, COUNT(*) AS total_sellers FROM sellers
WHERE seller_state IN ('FL','IN')
GROUP BY ROLLUP(seller_state, seller_city)
ORDER BY seller_name,seller_city; <-- you can use Order by in SQL after ROLLUP

The query above uses 2 columns with ROLLUP operator.

This query groups all the sellers by state and then city. It also counts number of sellers in each group. 

You'll see additional rows in the result set. That's because the summary rows are added for each state and a final summary row as well.

You can also use ORDER BY with ROLLUP in SQL. MySQL doesn't allow to do that.

To deal with null values, you can use the GROUPING function. I'll discuss about it in future article. 


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to create a Random Hex Color generator using JavaScript?JavaScriptclip and clip-path properties in CSSCSSControl Scrolling with CSS Scroll SnapCSSHow to uninstall Cocoapods from the Mac OS?MiscellaneousHow to use images instead of HTML radio buttons using CSS?CSSHow to define variables in SCSS (Sass)?SCSS