DuckDB ⟿ Grouping Sets

· 1 min read

SQL's GROUPING SETS allow you to compute aggregates over multiple dimensions in a single query! Say goodbye to writing multiple queries for different groups. This functionality streamlines your data analysis and saves time!

DuckDB

Go to: https://www.quackdb.com/.

Copy the code below and paste it into the editor. And run it.

WITH sales AS (
    SELECT * FROM read_csv_auto('https://raw.githubusercontent.com/creatuluw/datasets/refs/heads/main/global_superstore.csv')
)

SELECT 
    country, 
    segment,
    COUNT(DISTINCT customer_id) AS customer_count
FROM sales
GROUP BY 
    GROUPING SETS (
        (country),
        (country, segment), 
        (segment), 
        ()
    );

Then scroll down to see the different aggregations in your table.

© 2025 Patrick Tehubijuluw. All rights reserved.