Bird
0
0

Given the table SalesData with columns Product, Q1, Q2, Q3 and data:

medium📝 query result Q4 of 15
SQL - Advanced Query Patterns
Given the table SalesData with columns Product, Q1, Q2, Q3 and data:
Product | Q1 | Q2 | Q3
--------|----|----|---
Pen     | 10 | 15 | 20
Book    | 5  | 7  | 8

What will be the result of this query?
SELECT Product, Quarter, Amount FROM SalesData UNPIVOT (Amount FOR Quarter IN (Q1, Q2, Q3));
ARows with Product, Quarter (Q1, Q2, Q3), and Amount values for each quarter.
BColumns Q1, Q2, Q3 will be removed and only Product remains.
CAn error because UNPIVOT requires aggregation.
DThe original table unchanged.
Step-by-Step Solution
Solution:
  1. Step 1: Understand UNPIVOT effect

    UNPIVOT converts columns Q1, Q2, Q3 into rows with Quarter and Amount columns.
  2. Step 2: Predict output

    Each product will have 3 rows, one per quarter, showing the amount.
  3. Final Answer:

    Rows with Product, Quarter (Q1, Q2, Q3), and Amount values for each quarter. -> Option A
  4. Quick Check:

    UNPIVOT output = rows per column value [OK]
Quick Trick: UNPIVOT turns columns into multiple rows [OK]
Common Mistakes:
  • Expecting columns to remain
  • Thinking UNPIVOT needs aggregation
  • Confusing with PIVOT output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes