---
title: "SQL PIVOT Table – Transform Rows into Columns"  
description: "PIVOT operation is used to convert row data into columns. It’s extremely useful when you want to generate reports, summaries, or dashboards from normalized data"  
author: "Anubhav Sharma"  
published: 2026-04-27  
updated: 2026-04-28  
canonical: https://answers.mindstick.com/blog/242/sql-pivot-table-transform-rows-into-columns  
category: "database"  
tags: ["database", "sql server"]  
reading_time: 3 minutes  

---

# SQL PIVOT Table – Transform Rows into Columns

In SQL, the **PIVOT** operation is used to [convert](https://www.mindstick.com/blog/698/convert-text-document-to-pdf-file) row data into columns. It’s extremely useful when you want to generate [reports](https://www.mindstick.com/news/2587/according-to-reports-apple-is-developing-new-external-monitors-with-an-inside-chip), summaries, or dashboards from normalized data.

## What is PIVOT?

**PIVOT** rotates data from rows into columns, allowing you to [aggregate](https://www.mindstick.com/blog/52/aggregate-functions-in-database) values and display them in a more readable format.

- Think of it like creating a summary table where:
- Rows become columns
- Values are aggregated (SUM, COUNT, etc.)

## When to Use PIVOT

- Creating reports (monthly sales, yearly stats)
- Converting category rows into columns
- Dashboard data representation
- Data summarization

![SQL PIVOT Table – Transform Rows into Columns](https://answers.mindstick.com/blogs/4fd6d6c8-3f52-41cf-9479-ed1876e552d6/images/c9735a9f-866f-41ca-8a6a-44ae8d617432.png)

## Sample Table

## Sales Table:

| [Employee](https://www.mindstick.com/articles/85431/remote-employee-training-tips-tricks) | Month | Sales |
| --- | --- | --- |
| John | Jan | 100 |
| John | Feb | 150 |
| Mike | Jan | 200 |
| Mike | Feb | 250 |

## Without PIVOT (Normal Query)

```plaintext
SELECT Employee, Month, Sales
FROM Sales;
```

Output remains row-based.

## Using PIVOT

```plaintext
SELECT *
FROM (
    SELECT Employee, Month, Sales
    FROM Sales
) AS SourceTable
PIVOT (
    SUM(Sales)
    FOR Month IN ([Jan], [Feb])
) AS PivotTable;
```

## Output

| Employee | Jan | Feb |
| --- | --- | --- |
| John | 100 | 150 |
| Mike | 200 | 250 |

## Syntax Breakdown

```plaintext
PIVOT (
    AGGREGATE_FUNCTION(ColumnToAggregate)
    FOR ColumnToPivot IN ([Value1], [Value2], ...)
)
```

**AGGREGATE_FUNCTION** → SUM, COUNT, AVG, etc.

- **ColumnToAggregate** → numeric [column](https://www.mindstick.com/forum/2288/column-site_name-ambiguous-problem) (e.g., Sales)
- **ColumnToPivot** → column whose values become new columns (e.g., Month)
- **IN** → list of values to convert into columns

## Key Points

- PIVOT requires an **aggregate function**
- You must specify column values manually in `IN`
- Works mainly in **[SQL Server](https://www.mindstick.com/articles/34/create-table-in-microsoft-sql-server)** (syntax may differ in other DBs)

## Dynamic PIVOT (Advanced)

If you don’t know column names in advance (e.g., months), use **[dynamic SQL](https://www.mindstick.com/interview/623/what-is-dynamic-sql)**:

```plaintext
DECLARE @cols NVARCHAR(MAX), @query NVARCHAR(MAX);

SELECT @cols = STRING_AGG(QUOTENAME(Month), ',')
FROM (SELECT DISTINCT Month FROM Sales) AS M;

SET @query = '
SELECT *
FROM (
    SELECT Employee, Month, Sales FROM Sales
) AS Source
PIVOT (
    SUM(Sales) FOR Month IN (' + @cols + ')
) AS P;';

EXEC sp_executesql @query;
```

## PIVOT vs GROUP BY

| Feature | PIVOT | GROUP BY |
| --- | --- | --- |
| Output | Columns | Rows |
| Use Case | Reports | [Aggregation](https://www.mindstick.com/forum/34071/use-aggregation-operator-max-in-linq) |
| Flexibility | Less flexible | More flexible |

## Conclusion

SQL PIVOT is a powerful tool for transforming and summarizing data into a [report](https://www.mindstick.com/forum/1972/report-printing)-friendly format. While it requires a bit of setup, it can significantly [simplify complex](https://www.mindstick.com/forum/160190/how-is-cte-used-to-simplify-complex-sql-queries) reporting queries.

---

Original Source: https://answers.mindstick.com/blog/242/sql-pivot-table-transform-rows-into-columns

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
