What if your database could do all the adding up for you, perfectly and instantly?
Why ROLLUP for subtotals in MySQL? - Purpose & Use Cases
Imagine you run a small store and want to see sales totals by product category and also the overall total. You try to add up each category's sales by hand using a calculator or spreadsheet.
Doing this manually is slow and easy to mess up. You might forget to add some categories or make mistakes adding many numbers. It's hard to keep totals updated when new sales come in.
The ROLLUP feature in SQL automatically adds subtotal and grand total rows to your grouped data. It saves time and avoids errors by doing all the adding inside the database.
SELECT category, SUM(sales) FROM sales GROUP BY category; -- Then add totals manually
SELECT category, SUM(sales) FROM sales GROUP BY category WITH ROLLUP;
With ROLLUP, you can quickly get detailed and summary sales data in one simple query, making reports faster and more reliable.
A store manager can see sales by each product category and the total sales for the whole store in one report, helping make better stocking decisions.
Manual adding of subtotals is slow and error-prone.
ROLLUP automatically creates subtotals and grand totals in SQL queries.
This makes data summaries faster, easier, and more accurate.