Consider a sales table with columns region, product, and sales_amount. What is the output of this query?
SELECT region, product, SUM(sales_amount) AS total_sales FROM sales GROUP BY GROUPING SETS ((region), (product));
SELECT region, product, SUM(sales_amount) AS total_sales FROM sales GROUP BY GROUPING SETS ((region), (product));
GROUPING SETS allows you to specify multiple groupings separately.
The query groups rows separately by region and by product. Each grouping set produces rows with NULL in the other column.
Which statement best describes what GROUPING SETS ((a), (b)) does in a GROUP BY clause?
Think about how GROUPING SETS lets you specify multiple groupings.
GROUPING SETS ((a), (b)) produces two separate groupings: one by a and one by b.
Which option contains a syntax error in the GROUP BY clause using GROUPING SETS?
SELECT department, role, COUNT(*) FROM employees GROUP BY GROUPING SETS (department, (role));
GROUPING SETS requires each grouping set to be enclosed in parentheses.
Option B lacks parentheses around each grouping set, causing a syntax error.
You want to get total sales grouped by region, product, and also the grand total. Which GROUP BY clause is the most efficient and correct?
Think about how to include multiple groupings and the grand total.
Option A explicitly lists grouping sets for region, product, and the empty set for grand total.
Given the query:
SELECT region, product, SUM(sales) FROM sales_data GROUP BY GROUPING SETS ((region, product), (region), (product));
Why might some rows have NULL in both region and product columns?
Think about what produces the grand total row with NULLs in grouping columns.
Only the empty grouping set () produces a row with NULLs in all grouping columns representing the grand total.