SQL · Pattern 11 — Interview SQL Patterns
Pivot
Converts row values into columns.
Source data — Pattern 11: Interview SQL Patterns
All queries run against the shared employees table (see schema.sql). Topics 71 and 72 introduce an orders table to solve consecutive records and gaps-and-islands problems. Topics 73 and 74 introduce a monthly_sales table to demonstrate PIVOT and UNPIVOT operations.




What it does
Converts row values into columns.
Problem
Convert monthly sales rows into columns.
SELECT *
FROM monthly_sales
PIVOT(SUM(amount) FOR month IN ('Jan','Feb','Mar'));
Result
