thinQmaster Virtual testing partner Adaptive practice exams that find your weak spots and drill them. Every answer explained. From $69
Enroll

thinQmaster

Find your exam

Five questions free, before you decide.

Step 1 · choose a vendor

In one of our camps? Your practice exams come with the tuition — they are on your study page already.

Data & Analytics · Intermediate

PIVOT and UNPIVOT

SQL operators that rotate data: PIVOT turns row values into columns, and UNPIVOT turns columns into rows.

Definition

PIVOT aggregates values into new columns, one per category, producing a crosstab. UNPIVOT normalizes wide data such as one column per month into long (tidy) form. Both are supported in SQL Server, Oracle, Snowflake and Databricks; conditional aggregation with CASE is a portable alternative.

Example

SELECT * FROM sales PIVOT (SUM(amount) FOR quarter IN ('Q1','Q2','Q3','Q4'));

Exam tip

For BI tools, data with one column per period usually needs UNPIVOT into long form first.

Related terms