SQL - Advanced Query PatternsWhich 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;Check Answer
Step-by-Step SolutionSolution:Step 1: Review PIVOT syntaxThe correct syntax uses PIVOT with an aggregate function and FOR ... IN clause listing values.Step 2: Check optionsSELECT * FROM table PIVOT (SUM(sales) FOR year IN (2019, 2020)); matches the correct syntax. Others misuse UNPIVOT or have invalid clauses.Final Answer:SELECT * FROM table PIVOT (SUM(sales) FOR year IN (2019, 2020)); -> Option CQuick 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 PIVOTWrong clause orderMissing aggregate function
Master "Advanced Query Patterns" in SQL9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More SQL Quizzes CASE Expressions - Why CASE expressions are needed - Quiz 14medium CASE Expressions - CASE in ORDER BY - Quiz 14medium Indexes and Query Performance - How an index works (B-tree mental model) - Quiz 8hard Stored Procedures and Functions - Function vs procedure decision - Quiz 3easy Stored Procedures and Functions - Variables and SET statements - Quiz 1easy Transactions and Data Integrity - Why transactions are needed - Quiz 9hard Transactions and Data Integrity - Why transactions are needed - Quiz 7medium Triggers - INSERT trigger - Quiz 14medium Triggers - BEFORE trigger execution - Quiz 13medium Window Functions Fundamentals - Why window functions are needed - Quiz 6medium