Bird
0
0

Which of the following is the correct syntax to use PIVOT in SQL?

easy📝 Syntax Q3 of 15
SQL - Advanced Query Patterns
Which of the following is the correct syntax to use PIVOT in SQL?
ASELECT * FROM table UNPIVOT (SUM(sales) FOR year IN (2019, 2020));
BSELECT * FROM table WHERE PIVOT sales BY year;
CSELECT * FROM table PIVOT (SUM(sales) FOR year IN (2019, 2020));
DSELECT * FROM table GROUP BY PIVOT year;
Step-by-Step Solution
Solution:
  1. Step 1: Review PIVOT syntax

    The correct syntax uses PIVOT with an aggregate function and FOR ... IN clause listing values.
  2. Step 2: Check options

    SELECT * FROM table PIVOT (SUM(sales) FOR year IN (2019, 2020)); matches the correct syntax. Others misuse UNPIVOT or have invalid clauses.
  3. Final Answer:

    SELECT * FROM table PIVOT (SUM(sales) FOR year IN (2019, 2020)); -> Option C
  4. Quick Check:

    PIVOT syntax = SELECT * FROM table PIVOT (SUM(sales) FOR year IN (2019, 2020)); [OK]
Quick Trick: PIVOT uses FOR ... IN with aggregation [OK]
Common Mistakes:
  • Using UNPIVOT instead of PIVOT
  • Wrong clause order
  • Missing aggregate function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes