advanced

Unpivot Basics

6 min readLast updated: 2026-07-23

Overview

Unpivoting is the reverse of pivoting—it rotates column headers back into row attributes to normalize wide datasets.

Code Examples

SQL

sql
-- Unpivot wide metrics table using UNION ALL
SELECT year, 'Jan' AS month, Jan_Sales AS sales FROM wide_sales
UNION ALL
SELECT year, 'Feb' AS month, Feb_Sales AS sales FROM wide_sales;

Summary

Unpivoting normalizes wide multi-column tables back into tall row-oriented formats.