Revisiting GROUP BY ROLLUP with a more realistic example

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
Search for a command to run...

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
No comments yet. Be the first to comment.
Short, practical posts on SQL and BigQuery — from core language features to advanced query patterns. A reference for data practitioners at every level.
A few days ago I thought that the following SQL query would not work— I expected the window function result would be summed multiple times. 🚨 Turns out, I was wrong. This was a great reminder of why understanding SQL’s order of execution is crucial!...
Here's a useful Dataform concept: pre_operations and post_operations. As the name implies, these represent a set of actions that run before and after the main operation (table, view, or SQL operations

BigQuery has always been a SQL engine for tabular data. Object tables add an interesting twist to that. Instead of rows containing values, an object table gives you one row per file — pointing at da

Query your data lake with warehouse-grade security and performance — without moving a single file.

Ever run a heavy BigQuery SQL query, processed gigabytes of data — and then accidentally closed the tab or forgot to save the results? 😬 Don't re-run it. Your results are still there. BigQuery automa

You can use query parameters in BigQuery hashtag#SQL (now in the console as well!) — but how are they different from variables, and when should you use each? Both parameters and variables act as place

Ever had a random piece of knowledge from school suddenly click in a real-world scenario?
It felt like that for me remembering about ROLLUP a few days ago.
I wrote about GROUP BY ROLLUP roughly 1.5 years ago—one of my first posts here. At the time, it was unfamiliar to me, and I had no idea I’d ever need it. But this week, I finally encountered a real use case.
𝐓𝐡𝐞 𝐏𝐫𝐨𝐛𝐥𝐞𝐦
Imagine we have sales data for a retail store, where each product belongs to a subcategory and a category (e.g., Apples → Fruits → Food).
We want to compute the average ordered quantity per product, but with a hierarchical fallback:
𝐇𝐨𝐰 𝐑𝐎𝐋𝐋𝐔𝐏 𝐇𝐞𝐥𝐩𝐬
When we GROUP BY ROLLUP (category, subcategory, product_id), we get multiple aggregation levels in one query:
✅ Per product
✅ Per subcategory
✅ Per category
✅ Across all rows
This allows us to build a lookup table, which we can use with multiple LEFT JOINs to apply the fallback logic.
𝐋𝐞𝐭'𝐬 𝐭𝐞𝐬𝐭 𝐢𝐭
Here’s how it works in practice:
• Apples → Direct sales data → AVG(quantity) = 6
• Mangoes → No past sales → Uses Fruits subcategory → AVG(quantity) = 4.67
• Cucumbers → No past sales, no Vegetables subcategory data → Uses Food category → AVG(quantity) = 4.67
• Washing Machine → No sales data, no relevant category → Uses overall average → AVG(quantity) = 6

𝐈𝐧 𝐥𝐢𝐞𝐮 𝐨𝐟 𝐚 𝐜𝐨𝐧𝐜𝐥𝐮𝐬𝐢𝐨𝐧
This was a fun experiment, but let’s be honest—this could also be done with window functions!
Still, ROLLUP provides an perspective, and I’m on the lookout for an even better use case.
Have you ever had an SQL feature suddenly “click” for you?
Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.