Bird
0
0

Consider the table orders with columns country, category, and sales. What rows will the following query return?

medium📝 query result Q5 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Consider the table orders with columns country, category, and sales. What rows will the following query return?
SELECT country, category, SUM(sales) FROM orders GROUP BY CUBE(country, category);
ARows for each country-category pair, plus subtotals for each country, each category, and a grand total.
BRows only for each country-category pair without any subtotals.
CRows for each country and category separately but no grand total.
DRows grouped by country only, ignoring category.
Step-by-Step Solution
Solution:
  1. Step 1: Understand CUBE behavior

    CUBE generates all combinations of grouping columns, including individual columns and grand total.
  2. Step 2: Analyze the query

    The query groups by CUBE(country, category), so it returns rows for each country-category pair, subtotals by country, subtotals by category, and a grand total row.
  3. Final Answer:

    Rows for each country-category pair, plus subtotals for each country, each category, and a grand total. -> Option A
  4. Quick Check:

    CUBE returns all grouping combinations including grand total [OK]
Quick Trick: CUBE returns all grouping combinations including grand total [OK]
Common Mistakes:
  • Assuming CUBE returns only detailed rows without subtotals
  • Confusing CUBE with ROLLUP which has fewer subtotal rows
  • Ignoring the grand total row generated by CUBE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes