Introduction
ROLLUP helps you quickly get totals and subtotals in one query, saving time and effort.
SELECT column1, column2, ..., aggregate_function(column) FROM table_name GROUP BY column1, column2 WITH ROLLUP;
SELECT department, employee, SUM(sales) AS total_sales FROM sales_data GROUP BY department, employee WITH ROLLUP;
SELECT category, SUM(amount) AS total_amount FROM expenses GROUP BY category WITH ROLLUP;
CREATE TABLE sales ( region VARCHAR(20), product VARCHAR(20), amount INT ); INSERT INTO sales VALUES ('North', 'Apples', 100), ('North', 'Oranges', 150), ('South', 'Apples', 200), ('South', 'Oranges', 100); SELECT region, product, SUM(amount) AS total_amount FROM sales GROUP BY region, product WITH ROLLUP;
ROLLUP keyword do in a MySQL GROUP BY query?ROLLUP in a MySQL query?GROUP BY ... WITH ROLLUP.GROUP BY category WITH ROLLUP, which is correct syntax. Others misuse ROLLUP placement or syntax.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;
SELECT region, product, SUM(amount) FROM sales GROUP BY region, product ROLLUP;
GROUP BY ... WITH ROLLUP, not just appended after GROUP BY columns.GROUP BY region, product WITH ROLLUP to fix the error.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?Pass the test and this pattern moves unknown