ROLLUP for subtotals in MySQL - Time & Space Complexity
When using ROLLUP in SQL, we want to know how the time to get subtotals grows as the data grows.
We ask: How does adding more rows or grouping levels affect the work done?
Analyze the time complexity of the following code snippet.
SELECT category, subcategory, SUM(sales) AS total_sales
FROM sales_data
GROUP BY category, subcategory WITH ROLLUP;
This query groups sales by category and subcategory, then adds subtotal rows for each category and a grand total.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning all rows to aggregate sales.
- How many times: Once over all rows, then grouping and subtotal calculations for each group level.
As the number of rows grows, the database scans more data. Also, more groups mean more subtotal calculations.
| Input Size (n rows) | Approx. Operations |
|---|---|
| 10 | Scan 10 rows + subtotal for groups |
| 100 | Scan 100 rows + subtotal for groups |
| 1000 | Scan 1000 rows + subtotal for groups |
Pattern observation: The scan grows linearly with rows, and subtotal steps grow with number of groups, which depends on distinct categories and subcategories.
Time Complexity: O(n + g)
This means the work grows with the number of rows plus the number of groups for subtotals.
[X] Wrong: "ROLLUP just adds a fixed small cost regardless of data size."
[OK] Correct: The subtotal calculations depend on how many groups exist, which can grow with data size, so cost is not fixed.
Understanding how grouping and subtotals affect query time helps you explain performance in real projects and shows you think about data size impact.
"What if we added more grouping columns to the ROLLUP? How would the time complexity change?"
Practice
ROLLUP keyword do in a MySQL GROUP BY query?Solution
Step 1: Understand GROUP BY basics
GROUP BY groups rows by column values to aggregate data.Step 2: Role of ROLLUP in GROUP BY
ROLLUP adds extra rows showing subtotals and grand totals for grouped data.Final Answer:
It adds subtotal and total rows automatically in the grouped result. -> Option AQuick Check:
ROLLUP = subtotals and totals [OK]
- Thinking ROLLUP filters data
- Confusing ROLLUP with ORDER BY
- Assuming ROLLUP creates backups
ROLLUP in a MySQL query?Solution
Step 1: Recall correct ROLLUP syntax
In MySQL, ROLLUP is used asGROUP BY ... WITH ROLLUP.Step 2: Analyze each option
SELECT category, SUM(amount) FROM sales GROUP BY category WITH ROLLUP; usesGROUP BY category WITH ROLLUP, which is correct syntax. Others misuse ROLLUP placement or syntax.Final Answer:
SELECT category, SUM(amount) FROM sales GROUP BY category WITH ROLLUP; -> Option CQuick Check:
ROLLUP goes after GROUP BY with WITH keyword [OK]
- Placing ROLLUP before GROUP BY
- Using ROLLUP as a function
- Omitting WITH keyword
sales with columns region, product, and amount, what will be the output of this query?SELECT region, product, SUM(amount) AS total_sales FROM sales GROUP BY region, product WITH ROLLUP;
Solution
Step 1: Understand GROUP BY with multiple columns and ROLLUP
ROLLUP creates subtotals for each grouping level: first by region and product, then by region only, then grand total.Step 2: Interpret NULL values in output
Subtotal rows have NULL in the column(s) rolled up. The grand total row has NULL in both region and product.Final Answer:
Rows grouped by region and product, plus subtotals by region and a grand total row with NULLs. -> Option AQuick Check:
ROLLUP adds subtotal and total rows with NULL markers [OK]
- Expecting no subtotal rows
- Confusing grouping levels
- Thinking NULL means missing data
SELECT region, product, SUM(amount) FROM sales GROUP BY region, product ROLLUP;
What is the error and how to fix it?
Solution
Step 1: Identify syntax error in ROLLUP usage
ROLLUP must be used asGROUP BY ... WITH ROLLUP, not just appended after GROUP BY columns.Step 2: Correct the query syntax
Change query toGROUP BY region, product WITH ROLLUPto fix the error.Final Answer:
ROLLUP must be used with the keyword WITH after GROUP BY. -> Option DQuick Check:
ROLLUP requires WITH keyword [OK]
- Omitting WITH keyword
- Misplacing ROLLUP before GROUP BY
- Thinking ROLLUP disallows multiple columns
region and product, plus subtotals by region and a grand total. Which query correctly uses ROLLUP and filters out only the grand total row from the result?Solution
Step 1: Understand ROLLUP output rows
ROLLUP adds subtotal rows with NULLs in rolled-up columns; grand total row has NULL in all grouped columns.Step 2: Filter out only the grand total row
The grand total row has both region and product as NULL. To exclude it, use HAVING NOT (region IS NULL AND product IS NULL).Final Answer:
SELECT region, product, SUM(amount) FROM sales GROUP BY region, product WITH ROLLUP HAVING NOT (region IS NULL AND product IS NULL); -> Option BQuick Check:
Exclude grand total by filtering NULLs in all grouping columns [OK]
- Filtering only one column NULL
- Filtering to keep only grand total instead of excluding
- Using WHERE instead of HAVING with GROUP BY
Ready to prove it?
Pass the test and this pattern moves unknown
