Explore fundamental concepts and differences of GROUPING SETS, ROLLUP, and CUBE in SQL for advanced multi-level aggregations, using practical reporting scenarios and clear examples for effective query design.
Which SQL clause allows you to perform multiple groupings in a single query, such as totaling sales per city and per product at once?
Explanation: GROUPING SETS enables you to specify multiple groupings within the same query efficiently. ORDER GROUPS, GROUP FILTER, and GROUP NODES are not valid SQL clauses, making them incorrect options. Only GROUPING SETS provides this enhanced grouping functionality.
What is the main purpose of the ROLLUP extension in SQL aggregation queries?
Explanation: ROLLUP is designed to generate subtotals at each level of a hierarchy, culminating in a grand total, which is especially helpful for multi-level reports. Sorting data, removing duplicates, and filtering rows are not the functions of the ROLLUP extension; those refer to ORDER BY, DISTINCT, and WHERE clauses.
When using the CUBE keyword in a GROUP BY clause, what kind of groupings do you get?
Explanation: CUBE computes all possible combinations of the specified columns, including all subtotals and the grand total. Only totals for single columns would be a partial grouping, not a complete cube. 'No aggregation at all' and 'a filtered subset' don't relate to CUBE's functionality.
What is a common limitation of using the standard GROUP BY clause when generating multiple summarized reports in SQL?
Explanation: Using GROUP BY alone only supports a single grouping per query, requiring multiple queries for different aggregations. JOINs and sorting are still allowed with GROUP BY, and it can work with non-numeric columns for grouping purposes.
Which of the following shows a correct use of GROUPING SETS to total sales by city or product?
Explanation: The proper syntax for GROUPING SETS specifies the groupings inside double parentheses following GROUP BY. The other options misplace keywords or do not use the syntax that SQL requires.
In a sales dataset with columns city, product, and quarter, what result does ROLLUP(city, product) provide?
Explanation: ROLLUP(city, product) generates groupings for each city-product pair, then for each city, and finally a grand total. The second option only totals by product, not by city. The other two do not describe aggregation or summarize results as ROLLUP does.
Suppose you want to report total sales by product, by city, by quarter, and by every combination of these. Which SQL feature best achieves this?
Explanation: CUBE automatically computes all possible combinations of the specified grouping columns, ideal for multidimensional analysis. ORDER BY arranges the output, HAVING filters groups, and DISTINCT removes duplicates, none of which provide multi-level totals like CUBE.
Which grouping extension(s) automatically produce a grand total row in the query result?
Explanation: Both ROLLUP and CUBE, by design, create a final aggregate row representing the grand total. While GROUPING SETS can also produce a grand total if specified, it's not automatic. Standard GROUP BY does not create a grand total unless explicitly coded.
A hierarchical sales report requires subtotals by city and an overall total for all cities. Which SQL feature should you use?
Explanation: ROLLUP automatically summarizes data in a hierarchical fashion, ideal for subtotals by city and grand totals. JOIN combines tables, UNION merges result sets, and FILTER is not a standard SQL clause.
What is one advantage of GROUPING SETS over ROLLUP?
Explanation: GROUPING SETS gives you explicit control, letting you define any combination of groupings, not just hierarchical totals. The grand total row is not automatic; you must specify it. The other statements are either incorrect or unrelated to its core advantage.
Which aggregation clause is used in SQL to summarize data for one or more specified columns?
Explanation: GROUP BY groups the rows based on one or more columns for summarization. SELECT DISTINCT filters duplicates, MERGE is not a standard aggregation clause, and LIMIT restricts row counts.
Which of the following is NOT valid SQL syntax for GROUPING SETS, ROLLUP, or CUBE?
Explanation: GROUP BY GROUP SET (city, product) is not valid SQL; the correct keyword is GROUPING SETS. The other three options correctly use ROLLUP, CUBE, and GROUPING SETS syntax.
You are asked to generate totals per city, per product, both city and product, and an overall total. Which SQL clause can do all of these in a single query?
Explanation: GROUPING SETS lets you specify all required groupings (city, product, both, grand total) in one query. SELECT UNIQUE is not SQL standard, WHERE ALL is not valid here, and UNION combines different result sets, not groupings.
In results using ROLLUP or CUBE, what does a NULL value in a grouping column typically indicate?
Explanation: A NULL in a grouping column created by ROLLUP or CUBE usually signifies that the row is a subtotal or grand total. It is not necessarily a data error, join issue, or deleted record. The NULLs are used intentionally for aggregated rows.
How do GROUPING SETS, ROLLUP, and CUBE relate to query performance compared to traditional GROUP BY for many aggregations?
Explanation: GROUPING SETS, ROLLUP, and CUBE can improve performance by eliminating the need for separate queries, thus reducing overhead. While complex groupings can impact query time, the ability to combine aggregations in one query is typically more efficient. They work with more than just date columns and support joins.
Which result do you expect when applying GROUPING SETS ((city), (product)) to a sales table?
Explanation: GROUPING SETS ((city), (product)) produces two levels of aggregation: one for cities and one for products, but not their combinations. Summarizing by city-product pairs would require a different grouping set. The single grand total or no aggregation does not match this query.