Bird
Raised Fist0
SQLquery~20 mins

Pivot and unpivot concepts in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pivot and Unpivot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of a simple PIVOT query
Given the table Sales with columns Year, Product, and Amount, what is the output of this PIVOT query?
SELECT * FROM (
  SELECT Year, Product, Amount FROM Sales
) AS SourceTable
PIVOT (
  SUM(Amount) FOR Product IN ([A], [B])
) AS PivotTable
ORDER BY Year;
SQL
CREATE TABLE Sales (Year INT, Product VARCHAR(10), Amount INT);
INSERT INTO Sales VALUES
(2020, 'A', 100),
(2020, 'B', 150),
(2021, 'A', 200),
(2021, 'B', 250);
A
Year | Product | Amount
-----|---------|-------
2020 | A       | 100
2020 | B       | 150
2021 | A       | 200
2021 | B       | 250
B
Year | A   | B
-----|-----|----
2020 | 150 | 100
2021 | 250 | 200
C
Year | A   | B
-----|-----|----
2020 | NULL| NULL
2021 | NULL| NULL
D
Year | A   | B
-----|-----|----
2020 | 100 | 150
2021 | 200 | 250
Attempts:
2 left
💡 Hint
Remember that PIVOT aggregates values and arranges them as columns based on the specified IN list.
❓ query_result
intermediate
2:00remaining
Output of an UNPIVOT query
Given the table MonthlySales with columns Year, Jan, Feb, what is the output of this UNPIVOT query?
SELECT Year, Month, Sales FROM MonthlySales
UNPIVOT (
  Sales FOR Month IN (Jan, Feb)
) AS UnpivotTable
ORDER BY Year, Month;
SQL
CREATE TABLE MonthlySales (Year INT, Jan INT, Feb INT);
INSERT INTO MonthlySales VALUES
(2020, 100, 150),
(2021, 200, 250);
A
Year | Jan | Feb
-----|-----|----
2020 | 100 | 150
2021 | 200 | 250
B
Year | Month | Sales
-----|-------|------
2020 | Jan   | 100
2020 | Feb   | 150
2021 | Jan   | 200
2021 | Feb   | 250
C
Year | Month | Sales
-----|-------|------
2020 | Jan   | 150
2020 | Feb   | 100
2021 | Jan   | 250
2021 | Feb   | 200
D
Year | Month | Sales
-----|-------|------
2020 | NULL  | NULL
2021 | NULL  | NULL
Attempts:
2 left
💡 Hint
UNPIVOT converts columns into rows, so each month becomes a row with its sales.
🧠 Conceptual
advanced
1:30remaining
Understanding PIVOT aggregation behavior
Why does a PIVOT operation in SQL require an aggregation function like SUM or COUNT?
ABecause PIVOT combines multiple rows into one, aggregation summarizes values for each new column.
BBecause PIVOT only works on single rows and does not need aggregation.
CBecause aggregation is optional and only used for sorting results.
DBecause PIVOT duplicates rows without changing data, so aggregation is irrelevant.
Attempts:
2 left
💡 Hint
Think about what happens when multiple rows share the same pivot column value.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in this PIVOT query
Which option contains a syntax error in this PIVOT query?
SELECT * FROM (
  SELECT Year, Product, Amount FROM Sales
) AS SourceTable
PIVOT (
  SUM(Amount) FOR Product IN (A, B)
) AS PivotTable;
ASUM(Amount) should be replaced with COUNT(Amount)
BThe alias PivotTable is missing after PIVOT keyword
CThe IN list should have column names in square brackets: [A], [B]
DThe SELECT * should specify columns explicitly
Attempts:
2 left
💡 Hint
Check the syntax for the IN list in PIVOT clause.
❓ optimization
expert
3:00remaining
Optimizing UNPIVOT for large datasets
You have a large table with many monthly sales columns (Jan to Dec). Which approach optimizes performance when unpivoting these columns into rows?
AUse CROSS APPLY with VALUES clause to unpivot columns efficiently
BUse multiple UNION ALL queries to select each month separately and combine
CCreate a normalized table with month and sales columns and join it instead of unpivoting
DUse UNPIVOT operator directly on all 12 columns in one query
Attempts:
2 left
💡 Hint
Consider SQL Server's CROSS APPLY with VALUES for better performance than UNPIVOT.

Practice

(1/5)
1. What is the main purpose of the PIVOT operation in SQL?
easy
A. To convert rows into columns for easier data summarization
B. To convert columns into rows to list data
C. To delete duplicate rows from a table
D. To join two tables based on a common column

Solution

  1. Step 1: Understand what pivot does

    PIVOT changes the orientation of data by turning row values into columns.
  2. Step 2: Compare with other options

    Only To convert rows into columns for easier data summarization correctly describes this; B describes unpivot, C and D are unrelated.
  3. Final Answer:

    To convert rows into columns for easier data summarization -> Option A
  4. Quick Check:

    Pivot = rows to columns [OK]
Hint: Pivot flips rows to columns; unpivot does the reverse [OK]
Common Mistakes:
  • Confusing pivot with unpivot
  • Thinking pivot deletes data
  • Mixing pivot with join operations
2. Which of the following is the correct syntax to unpivot columns Q1, Q2, Q3 into rows in SQL Server?
easy
A. SELECT Product, Quarter, Sales FROM SalesData UNPIVOT (Sales IN (Q1, Q2, Q3)) AS Unpvt
B. SELECT Product, Quarter, Sales FROM SalesData UNPIVOT (Sales FOR Quarter IN ([Q1], [Q2], [Q3])) AS Unpvt
C. SELECT Product, Quarter, Sales FROM SalesData UNPIVOT (Quarter FOR Sales IN (Q1, Q2, Q3)) AS Unpvt
D. SELECT Product, Quarter, Sales FROM SalesData PIVOT (Sales FOR Quarter IN (Q1, Q2, Q3)) AS Pvt

Solution

  1. Step 1: Recall UNPIVOT syntax

    UNPIVOT requires specifying the value column and the column that holds the original column names (FOR clause).
  2. Step 2: Check each option

    SELECT Product, Quarter, Sales FROM SalesData UNPIVOT (Sales FOR Quarter IN ([Q1], [Q2], [Q3])) AS Unpvt correctly uses UNPIVOT (Sales FOR Quarter IN ([Q1], [Q2], [Q3])). Others misuse PIVOT or omit FOR clause.
  3. Final Answer:

    SELECT Product, Quarter, Sales FROM SalesData UNPIVOT (Sales FOR Quarter IN ([Q1], [Q2], [Q3])) AS Unpvt -> Option B
  4. Quick Check:

    UNPIVOT syntax = SELECT Product, Quarter, Sales FROM SalesData UNPIVOT (Sales FOR Quarter IN ([Q1], [Q2], [Q3])) AS Unpvt [OK]
Hint: UNPIVOT needs 'value FOR name IN (columns)' syntax [OK]
Common Mistakes:
  • Using PIVOT instead of UNPIVOT
  • Swapping value and FOR column names
  • Omitting the FOR clause
3. Given the table Sales with columns Product, Year, Quarter, Amount, what will be the result of this query?
SELECT * FROM Sales PIVOT (SUM(Amount) FOR Quarter IN ([Q1], [Q2], [Q3], [Q4])) AS Pvt;
medium
A. An error because SUM cannot be used in PIVOT
B. A table with columns Product, Year, Quarter, Amount unchanged
C. A table with columns Product, Year, Q1, Q2, Q3, Q4 showing summed amounts per quarter
D. A table with columns Quarter, Product, Year, Amount

Solution

  1. Step 1: Understand PIVOT with aggregation

    PIVOT with SUM aggregates Amount values grouped by Product and Year, creating columns for each Quarter.
  2. Step 2: Analyze query output

    The output will have Product, Year, and one column per quarter (Q1 to Q4) with summed amounts.
  3. Final Answer:

    A table with columns Product, Year, Q1, Q2, Q3, Q4 showing summed amounts per quarter -> Option C
  4. Quick Check:

    PIVOT with SUM aggregates rows into columns [OK]
Hint: PIVOT with SUM groups and sums data into new columns [OK]
Common Mistakes:
  • Expecting original rows unchanged
  • Thinking PIVOT cannot use aggregation
  • Confusing column order in output
4. You wrote this query to unpivot sales data:
SELECT Product, Quarter, Sales FROM SalesData UNPIVOT (Sales FOR Quarter IN (Q1, Q2, Q3)) AS Unpvt;

But it returns an error. What is the likely cause?
medium
A. The FOR clause should be after the IN clause
B. UNPIVOT cannot be used with more than two columns
C. The alias 'Unpvt' is missing parentheses
D. The column names in IN clause must be enclosed in square brackets

Solution

  1. Step 1: Check syntax for column names in UNPIVOT

    In SQL Server, column names in the IN list must be enclosed in square brackets if they are identifiers like Q1, Q2, Q3.
  2. Step 2: Identify error cause

    Missing brackets cause syntax error; other options are incorrect syntax rules.
  3. Final Answer:

    The column names in IN clause must be enclosed in square brackets -> Option D
  4. Quick Check:

    Use brackets for column names in UNPIVOT IN list [OK]
Hint: Always bracket column names in UNPIVOT IN list [OK]
Common Mistakes:
  • Omitting brackets around column names
  • Misplacing FOR clause
  • Incorrect alias syntax
5. You have a table MonthlySales with columns Store, Jan, Feb, Mar representing sales per month. You want to create a report listing Store, Month, Sales rows. Which SQL approach correctly unpivots this data?
hard
A. Use UNPIVOT: SELECT Store, Month, Sales FROM MonthlySales UNPIVOT (Sales FOR Month IN ([Jan], [Feb], [Mar])) AS Unpvt;
B. Use PIVOT: SELECT Store, Month, Sales FROM MonthlySales PIVOT (SUM(Sales) FOR Month IN ([Jan], [Feb], [Mar])) AS Pvt;
C. Use JOIN to combine Jan, Feb, Mar columns into rows
D. Use GROUP BY Store, Month without unpivot

Solution

  1. Step 1: Identify the goal

    We want to transform columns Jan, Feb, Mar into rows with Month and Sales columns.
  2. Step 2: Choose correct SQL operation

    UNPIVOT converts columns into rows; Use UNPIVOT: SELECT Store, Month, Sales FROM MonthlySales UNPIVOT (Sales FOR Month IN ([Jan], [Feb], [Mar])) AS Unpvt; uses correct syntax for this.
  3. Step 3: Evaluate other options

    PIVOT aggregates rows into columns (wrong direction), JOIN is complex and unnecessary, GROUP BY alone doesn't reshape columns.
  4. Final Answer:

    Use UNPIVOT: SELECT Store, Month, Sales FROM MonthlySales UNPIVOT (Sales FOR Month IN ([Jan], [Feb], [Mar])) AS Unpvt; -> Option A
  5. Quick Check:

    Unpivot columns to rows = Use UNPIVOT: SELECT Store, Month, Sales FROM MonthlySales UNPIVOT (Sales FOR Month IN ([Jan], [Feb], [Mar])) AS Unpvt; [OK]
Hint: Unpivot columns to rows using UNPIVOT with FOR and IN [OK]
Common Mistakes:
  • Using PIVOT instead of UNPIVOT
  • Trying to reshape with GROUP BY only
  • Overcomplicating with JOINs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard