Complete the code to select the total sales grouped by region and product using GROUPING SETS.
SELECT region, product, SUM(sales) FROM sales_data GROUP BY [1];The GROUPING SETS clause allows grouping by multiple sets of columns separately. Here, grouping by region and product individually is done by GROUPING SETS ((region), (product)).
Complete the code to group sales by region and product, and also by region alone using GROUPING SETS.
SELECT region, product, SUM(sales) FROM sales_data GROUP BY [1];This GROUPING SETS clause groups by both (region, product) and (region) separately, giving totals per product per region and totals per region.
Fix the error in the GROUP BY clause to correctly use GROUPING SETS for grouping by product and year.
SELECT product, year, SUM(amount) FROM sales GROUP BY [1];GROUPING SETS requires each grouping set to be enclosed in parentheses. So to group by product and year separately, use GROUPING SETS ((product), (year)).
Fill both blanks to group sales by region and product, and also by product alone using GROUPING SETS.
SELECT region, product, SUM(sales) FROM sales_data GROUP BY [1], [2];
GROUPING SETS can be used in the GROUP BY clause as a single argument. Here, both blanks should be filled with the same GROUPING SETS clause to form a valid GROUP BY.
Fill all three blanks to select region, product, and year with total sales grouped by these combinations using GROUPING SETS.
SELECT [1], [2], [3], SUM(sales) FROM sales_data GROUP BY GROUPING SETS ((region, product), (region, year), (product, year));
The SELECT clause must list the columns used in the GROUPING SETS: region, product, and year. The sales column is aggregated, so it is not selected directly.