Skip to content
← Blog
🔒 All in your browser 🚫 No uploads stored
Guide

SQL UNPIVOT: How to Rotate Columns Back into Rows

Bill Crawford — Guide — February 2026 — 8 min read  ·  Last updated January 26, 2026
Contents
  1. What is UNPIVOT?
  2. When to unpivot
  3. Using the generator
  4. UNPIVOT vs UNION ALL
  5. Using CROSS APPLY VALUES
  6. Worked examples

Generate an UNPIVOT query from your column list.

Connect on LinkedIn →

Open UNPIVOT Generator ↗

What Is UNPIVOT?

UNPIVOT is the reverse of PIVOT. Where PIVOT converts rows into columns, UNPIVOT converts columns back into rows — transforming a wide, denormalized table into a tall, normalized one. SQL Server has a native UNPIVOT operator; other databases use UNION ALL or CROSS APPLY (VALUES ...) to achieve the same result.

-- Wide (pivoted) table:
Region | Q1    | Q2    | Q3    | Q4
-------|-------|-------|-------|------
East   | 12400 | 15600 | 18200 | 21000
West   | 9800  | 11200 | 14500 | 16800

-- After UNPIVOT: normalized
Region | Quarter | Revenue
-------|---------|--------
East   | Q1      | 12400
East   | Q2      | 15600
...

When to Unpivot

Using the Generator

1
Enter your table name

The source table with the wide format you want to unpivot.

2
Enter the column(s) to keep as rows

These are the identifier columns that stay as-is — e.g. Region, Product.

3
Enter the columns to unpivot

List the column names that will become values in a new 'category' column — e.g. Q1, Q2, Q3, Q4.

4
Name the output columns

Give names to the new key column (e.g. Quarter) and value column (e.g. Revenue).

5
Choose your dialect

SQL Server uses native UNPIVOT syntax; other databases get a UNION ALL or CROSS APPLY equivalent.

UNPIVOT vs UNION ALL

The native UNPIVOT operator is more concise but only works in SQL Server. The UNION ALL approach works in every database but becomes verbose for many columns:

-- SQL Server UNPIVOT
SELECT Region, Quarter, Revenue
FROM sales_wide
UNPIVOT (Revenue FOR Quarter IN ([Q1],[Q2],[Q3],[Q4])) AS u;

-- UNION ALL (all databases)
SELECT Region, 'Q1' AS Quarter, Q1 AS Revenue FROM sales_wide
UNION ALL
SELECT Region, 'Q2', Q2 FROM sales_wide
UNION ALL
SELECT Region, 'Q3', Q3 FROM sales_wide
UNION ALL
SELECT Region, 'Q4', Q4 FROM sales_wide;

Using CROSS APPLY VALUES (SQL Server)

A cleaner alternative that avoids repeating the table name is CROSS APPLY (VALUES ...):

SELECT r.Region, v.Quarter, v.Revenue
FROM sales_wide r
CROSS APPLY (VALUES
  ('Q1', r.Q1),
  ('Q2', r.Q2),
  ('Q3', r.Q3),
  ('Q4', r.Q4)
) AS v(Quarter, Revenue);

Tip: CROSS APPLY VALUES is often more readable than both UNPIVOT and UNION ALL, especially when unpivoting many columns, and it handles NULLs more predictably than UNPIVOT (which silently removes NULL rows).

Worked Example

Monthly budget table with columns Dept, Jan, Feb, Mar — unpivot to Dept, Month, Budget:

SELECT Dept, Month, Budget
FROM budget
UNPIVOT (Budget FOR Month IN ([Jan],[Feb],[Mar])) AS u
ORDER BY Dept, Month;

Frequently Asked Questions

Does UNPIVOT remove NULL values?
Yes — SQL Server's native UNPIVOT operator silently excludes rows where the unpivoted value is NULL. If you need to preserve NULLs, use CROSS APPLY VALUES instead, which keeps NULL rows.
What's the PostgreSQL equivalent of UNPIVOT?
PostgreSQL uses CROSS JOIN LATERAL with a VALUES expression, or you can use UNION ALL. The unnest() function works for array data but not for column-to-row conversion.
Can I unpivot multiple value columns at once?
SQL Server's UNPIVOT handles one value column at a time. For multiple value columns (e.g. Budget and Actual), use CROSS APPLY VALUES with multiple value columns in each row.
Is UNPIVOT reversible?
Yes — you can PIVOT the output of UNPIVOT to get back to the original shape. This round-trip is useful for validating transformations.

Related Tools

BC
Bill Crawford
Founder, Data Conversion Center

Bill Crawford is a data systems developer and technical founder with over 30 years of professional experience in accounting, finance, and business operations.

He holds a Bachelor's degree in Accounting and has spent more than three decades working within financial and operational environments. Over the past 10 years, he has been heavily involved in the development, implementation, and refinement of financial and enterprise data systems for both Fortune 500 companies and smaller organizations.

His work bridges finance and technology — combining deep domain knowledge in structured reporting and accounting workflows with hands-on SQL development and database architecture experience.

Bill founded DataConversionCenter.com to build practical, browser-based tools that simplify complex data challenges, including:

Rather than focusing on theoretical examples, his tools and articles are informed by real-world challenges encountered in enterprise reporting systems, financial databases, and operational data environments.

Professional Background

Bill's mission is to reduce friction in data workflows — particularly for professionals working with structured financial, operational, and reporting data.