77. Unpivot Subject Scores into Rows
MediumPivotingUNION ALLunpivot
The reverse of pivoting turns columns into rows. With no native UNPIVOT, you SELECT each column as a (label, value) pair and stack the selects with UNION ALL.
Task: the report table stores one row per student with three subject columns. Unpivot it so there is one row per student per subject. Return student, subject, score, where subject is the literal 'math', 'science', or 'english'. Order by student, then by subject.
Output columns, in order: student, subject, score.
Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…
This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases — 5test cases in all. A sloppy query that only fits the example won't pass.
SQL· SQLite · runs in your browser
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.