Skip to main content

Fun with grouping sets and rollup in duckdb

Author

Joram Mutenge

Published

2026-11-28

For a long time, I didn’t understand GROUPING SETS and ROLLUP in SQL. However, after reading the DuckDB documentation, I finally understand how they work. If I found these concepts confusing, there are probably others who do too. In this article, I’ll explain how GROUPING SETS and ROLLUP work, and introduce another operation called CUBE.

The dataset

I’ll use a dataset that should be familiar to my regular readers: the clothing store sales dataset. I’ll start by creating a temporary table called sales to store all 1,000 rows of data.

CREATE TEMP TABLE sales AS (
SELECT * EXCLUDE("Account Number","Account Name")
FROM read_csv_auto('https://raw.githubusercontent.com/jorammutenge/learn-rust/refs/heads/main/sample_sales.csv'));

Now, let’s take a look at the data.

FROM sales;
┌──────────┬──────────┬──────────┬────────────┬───────────┬─────────────────────┐
│   sku    │ category │ quantity │ unit price │ ext price │        date         │
│ varchar  │ varchar  │  int64   │   double   │  double   │      timestamp      │
├──────────┼──────────┼──────────┼────────────┼───────────┼─────────────────────┤
│ HX-24728 │ Hat      │        1 │      98.98 │     98.98 │ 2014-09-28 11:56:02 │
│ LK-02338 │ Sweater  │        9 │       34.8 │     313.2 │ 2014-04-24 16:51:22 │
│ ZC-07383 │ Sweater  │       12 │      60.24 │    722.88 │ 2014-09-17 17:26:22 │
│ QS-76400 │ Sweater  │        5 │      15.25 │     76.25 │ 2014-01-30 07:34:02 │
│ RU-25060 │ Sweater  │       19 │      51.83 │    984.77 │ 2014-08-24 06:18:12 │
│    ·     │    ·     │        · │        ·   │       ·   │          ·          │
│    ·     │    ·     │        · │        ·   │       ·   │          ·          │
│    ·     │    ·     │        · │        ·   │       ·   │          ·          │
│ FT-50146 │ Sweater  │        2 │      46.48 │     92.96 │ 2014-08-10 16:20:32 │
│ IC-59308 │ Socks    │       19 │      29.25 │    555.75 │ 2013-11-20 13:32:45 │
│ IC-59308 │ Socks    │       18 │      54.79 │    986.22 │ 2014-03-10 08:11:59 │
│ RU-25060 │ Sweater  │       15 │      62.53 │    937.95 │ 2014-04-11 02:50:03 │
│ LK-02338 │ Sweater  │       11 │       86.4 │     950.4 │ 2014-02-14 20:10:42 │
└──────────┴──────────┴──────────┴────────────┴───────────┴─────────────────────┘
  1000 rows (10 shown)        use .last to show entire result         6 columns

The data has been successfully loaded into the sales table. However, some of the column names contain spaces, which makes them a little cumbersome to work with. I’ll rename those columns to remove the spaces.

ALTER TABLE sales RENAME COLUMN "unit price" TO unit_price;
ALTER TABLE sales RENAME COLUMN "ext price" TO ext_price;

Grouping sets

GROUPING SETS is a special type of GROUP BY that allows you to explicitly specify the levels of aggregation you want. It’s easiest to understand when viewed as a hierarchy. Consider the following business question: What are the sales by product category and year, the sales by year, and the total sales across the entire store?

The query below answers that question.

SELECT
    year(date) AS sales_year,
    category,
    SUM(ext_price) AS revenue
FROM sales
GROUP BY GROUPING SETS (
    (sales_year, category),
    (sales_year),
    ()
)
ORDER BY sales_year NULLS FIRST,
         category NULLS FIRST;
┌────────────┬──────────┬────────────────────┐
│ sales_year │ category │      revenue       │
│   int64    │ varchar  │       double       │
├────────────┼──────────┼────────────────────┤
│       NULL │ NULL     │          570179.94 │
│       2013 │ NULL     │          144831.94 │
│       2013 │ Hat      │ 30468.799999999996 │
│       2013 │ Socks    │ 41042.409999999996 │
│       2013 │ Sweater  │           73320.73 │
│       2014 │ NULL     │           425348.0 │
│       2014 │ Hat      │           68825.21 │
│       2014 │ Socks    │ 128127.52000000003 │
│       2014 │ Sweater  │ 228395.26999999996 │
└────────────┴──────────┴────────────────────┘

The query above produces a hierarchy with three levels:

  1. At the bottom of the hierarchy is the grand aggregation, represented by (). This gives us the total sales across the entire dataset. In the query result, this is the row where both sales_year and category are NULL.
  2. The second level is represented by (sales_year). This gives us the total sales for each year, 2013 and 2014. In the result set, these are the rows where sales_year has a value and category is NULL.
  3. The third level is represented by (sales_year, category). This gives us the total sales for each category within each year. These are the rows where neither column contains NULL values.

Let’s answer another business question: How many items were sold by SKU, by category, and across the entire store?

SELECT
    category,
    sku,
    SUM(quantity) AS units_sold
FROM sales
GROUP BY GROUPING SETS (
    (category, sku),
    (category),
    ()
)
ORDER BY category NULLS FIRST,
         sku NULLS FIRST;
┌──────────┬──────────┬────────────┐
│ category │   sku    │ units_sold │
│ varchar  │ varchar  │   int128   │
├──────────┼──────────┼────────────┤
│ NULL     │ NULL     │      10565 │
│ Hat      │ NULL     │       1889 │
│ Hat      │ HX-24728 │        756 │
│ Hat      │ XX-25746 │       1133 │
│ Socks    │ NULL     │       3125 │
│ Socks    │ AE-95093 │       1062 │
│ Socks    │ GS-95639 │        823 │
│ Socks    │ IC-59308 │       1240 │
│ Sweater  │ NULL     │       5551 │
│ Sweater  │ FT-50146 │        995 │
│ Sweater  │ LK-02338 │       1036 │
│ Sweater  │ QS-76400 │       1209 │
│ Sweater  │ RU-25060 │       1167 │
│ Sweater  │ ZC-07383 │       1144 │
└──────────┴──────────┴────────────┘
  14 rows                3 columns

Finally, here’s another business question your boss is likely to ask regularly: How does revenue break down by month, by year, and across the entire store?

SELECT
    year(date) AS sales_year,
    month(date) AS sales_month,
    SUM(ext_price) AS revenue
FROM sales
GROUP BY GROUPING SETS (
    (1, 2),
    (1),
    ()
)
ORDER BY 1 NULLS FIRST,
         2 NULLS FIRST;
┌────────────┬─────────────┬────────────────────┐
│ sales_year │ sales_month │      revenue       │
│   int64    │    int64    │       double       │
├────────────┼─────────────┼────────────────────┤
│       NULL │        NULL │          570179.94 │
│       2013 │        NULL │ 144831.94000000003 │
│       2013 │           9 │             543.24 │
│       2013 │          10 │  40943.06000000002 │
│       2013 │          11 │  40545.57000000001 │
│       2013 │          12 │           62800.07 │
│       2014 │        NULL │ 425347.99999999994 │
│       2014 │           1 │  45818.04999999999 │
│       2014 │           2 │  43784.77000000001 │
│       2014 │           3 │            44237.5 │
│       2014 │           4 │  48084.74999999999 │
│       2014 │           5 │  38486.70999999999 │
│       2014 │           6 │           36714.16 │
│       2014 │           7 │  52599.61999999999 │
│       2014 │           8 │  61112.51999999998 │
│       2014 │           9 │ 54509.920000000006 │
└────────────┴─────────────┴────────────────────┘
  16 rows                             3 columns
Tip

Just as with a regular GROUP BY, you can use column positions to specify the columns involved in GROUPING SETS. For example, (1, 2) is equivalent to (sales_year, sales_month).

Rollup

The ROLLUP operation produces the same results as GROUPING SETS. However, instead of explicitly listing all the aggregation levels, as we did in the queries above, ROLLUP automatically generates a hierarchy from left to right.

The query below answers the same question as our very first query, but this time using ROLLUP.

SELECT
    year(date) AS sales_year,
    category,
    SUM(ext_price) AS revenue
FROM sales
GROUP BY ROLLUP (sales_year, category)
ORDER BY sales_year NULLS FIRST,
         category NULLS FIRST;
┌────────────┬──────────┬────────────────────┐
│ sales_year │ category │      revenue       │
│   int64    │ varchar  │       double       │
├────────────┼──────────┼────────────────────┤
│       NULL │ NULL     │          570179.94 │
│       2013 │ NULL     │ 144831.93999999997 │
│       2013 │ Hat      │ 30468.799999999996 │
│       2013 │ Socks    │ 41042.409999999996 │
│       2013 │ Sweater  │           73320.73 │
│       2014 │ NULL     │           425348.0 │
│       2014 │ Hat      │           68825.21 │
│       2014 │ Socks    │ 128127.52000000003 │
│       2014 │ Sweater  │ 228395.26999999996 │
└────────────┴──────────┴────────────────────┘

As expected, the result is the same. You could say that, in DuckDB, ROLLUP is syntactic sugar for GROUPING SETS.

Cube

Unlike ROLLUP and GROUPING SETS, CUBE generates every possible combination of aggregations. To see how it works, let’s modify the business question from our first example:

What is the revenue by year and category, by year regardless of category, by category regardless of year, and across the entire store?

SELECT
    year(date) AS sales_year,
    category,
    SUM(ext_price) AS revenue
FROM sales
GROUP BY CUBE(sales_year, category)
ORDER BY sales_year NULLS FIRST,
         category NULLS FIRST;
┌────────────┬──────────┬────────────────────┐
│ sales_year │ category │      revenue       │
│   int64    │ varchar  │       double       │
├────────────┼──────────┼────────────────────┤
│       NULL │ NULL     │          570179.94 │
│       NULL │ Hat      │  99294.01000000001 │
│       NULL │ Socks    │ 169169.93000000002 │
│       NULL │ Sweater  │ 301715.99999999994 │
│       2013 │ NULL     │          144831.94 │
│       2013 │ Hat      │ 30468.799999999996 │
│       2013 │ Socks    │ 41042.409999999996 │
│       2013 │ Sweater  │           73320.73 │
│       2014 │ NULL     │           425348.0 │
│       2014 │ Hat      │           68825.21 │
│       2014 │ Socks    │ 128127.52000000003 │
│       2014 │ Sweater  │ 228395.26999999996 │
└────────────┴──────────┴────────────────────┘
  12 rows                          3 columns

Notice that the result contains 12 rows, compared with the 9 rows produced by the query using ROLLUP. This is because CUBE generates four levels of aggregation:

  • Year and category
  • Year only
  • Category only
  • Grand total

ROLLUP does not include the “Category only” aggregation level in its hierarchy.

I hope I’ve cleared up some of the confusion surrounding these SQL concepts, or at least made them a little easier to understand. I certainly have a much better understanding of them now. Let me know if you do too.