---
title: "How to create PIVOT table in SQL Server?"  
description: "How to create PIVOT table in SQL Server?"  
author: "Steilla Mitchel"  
published: 2023-12-19  
updated: 2025-01-23  
canonical: https://www.mindstick.com/forum/160541/how-to-create-pivot-table-in-sql-server  
category: "mssql server"  
tags: ["pivot", "sql server", "sql"]  
reading_time: 2 minutes  

---

# How to create PIVOT table in SQL Server?

How to create [PIVOT](https://www.mindstick.com/articles/1511/pivot-with-dynamic-columns-in-sql-server) [table in SQL](https://www.mindstick.com/forum/205/find-the-all-column-with-schema-for-any-table-in-sql-server) [Server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over)?

## Replies

### Reply by Khushi Singh

`PIVOT` operation is used in generating `PIVOT` [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) in [SQL server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) to change the rows in a table and turn them to columns of the same table for easy analyze and summate data. This is very useful when you need to reorganize data for purposes of reporting or comparison. First of all, your data should be presented in the way where one column gives values which would transform into new columns, the second column contains values that would be summarized, and the third and further columns offer context or buckets for the first two columns.

It starts with a base query by which the raw data is requested from the server. For instance, using the Sales table that consists of columns such as Product, Month, and Sales Amount, the initial query would retrieve these fields. After this, you define the `PIVOT operation`. This means defining an aggregate function such as `SUM` or `COUNT`, identifying the column’s values to become the new columns (such as Month) and listing the customized values in that column for instance, ‘Jan’, ‘Feb’, ‘Mar’.

Specifically, the `PIVOT operator`syntax demands that the subquery servers as the source data. Here again in this subquery you only pickup the columns required for the transformation stage. The `PIVOT` block then performs the aggregation and convert the row values to columns depending on the values which are indicated in the `IN clause`. For instance, when using monthly data and pivoting sales on a month basis then, the data output obtained would show the total sales for each product for the listed months.

This creates a table where each row corresponds to a product type, while Columns to the right represent pivoted columns with total quantity amount or any other hierarchical value for each month. SQL Server, for example, fills missing values in some or all columns with `NULL`, which is then changed with a default value of, for instance, 0 as provided by `COALESCE` or `ISNULL` functions. Unlike the basic `PIVOT` table which needs the user to specify the new column, you can use dynamical SQL when the new columns value is not.


---

Original Source: https://www.mindstick.com/forum/160541/how-to-create-pivot-table-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
