---
title: "Define the PIVOT Table with examples in SQL server."  
description: "PIVOT table in SQL Server is a technique used to transform data from rows into columns and aggregate the values ​​as required."  
author: "Ashutosh Patel"  
published: 2024-07-05  
updated: 2024-07-08  
canonical: https://www.mindstick.com/articles/336334/define-the-pivot-table-with-examples-in-sql-server  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql", "sqlite"]  
reading_time: 2 minutes  

---

# Define the PIVOT Table with examples in SQL server.

#### SQL Server PIVOT

A PIVOT [table in SQL](https://www.mindstick.com/forum/156792/how-to-prevent-to-deny-drop-permission-of-a-table-in-sql-server) Server is a way to convert data from rows to columns, grouping values ​​as needed. It is particularly useful for collecting and analyzing data in a readable format.

## Syntax-

```javascript
SELECT *
FROM (
   -- Subquery that retrieves the data to pivot
   SELECT <non-pivoted column(s)>,
          <pivot column>,
          <value column>
   FROM <source_table>
) AS SourceTable
PIVOT (
   -- PIVOT function parameters
   <aggregate_function>(<value column>)
   FOR <pivot column> IN (<column1>, <column2>, ... <columnN>)
) AS PivotTable;
```

## Example-

Let's see we have a sample [SQL table](https://answers.mindstick.com/qa/93955/how-to-select-top-row-value-from-sql-table-using-command) “**EmployeeDetails**” that contains information about [Employees](https://yourviews.mindstick.com/view/84543/mass-layoffs-in-2023-problem-for-employees),

![Define the PIVOT Table with examples in SQL server.](https://www.mindstick.com/mindstickarticle/dc725a68-f403-43d4-abb0-833e03b848d7/images/2b774c58-fbc6-4269-9436-fc223b0d60d6.png)

Now, use the **[SQL PIVOT](https://answers.mindstick.com/blog/242/sql-pivot-table-transform-rows-into-columns)** to count all **Male** and **Female** Employees in each location,

![Define the PIVOT Table with examples in SQL server.](https://www.mindstick.com/mindstickarticle/dc725a68-f403-43d4-abb0-833e03b848d7/images/ce53b257-0b3e-4300-a212-7c59ab060c28.png)

**Note:**\
**[Aggregation](https://www.mindstick.com/forum/217/what-is-aggregation-and-how-it-maps-into-a-java-class) Functions**: The most common aggregation functions used in PIVOT are `SUM`, `COUNT`, `AVG`, `MIN`, and `MAX`.

\
**Dynamic Pivot**: If the values ​​of the pivot column (ProductName, TransactionDate in the example above) are already dynamic or unknown, you may need to use [dynamic SQL](https://www.mindstick.com/interview/623/what-is-dynamic-sql) to create the pivot column dynamically

\
Using **PIVOT** in SQL Server can greatly simplify [data analysis](https://answers.mindstick.com/qa/106029/how-to-implement-google-analytics-custom-reports-for-detailed-data-analysis) tasks where data needs to be summarized and presented in a cross-[tabular format](https://www.mindstick.com/forum/34668/how-to-embed-tabular-format-data-in-outlook-using-asp-dot-net-mvc). Change the instances based on your specific [data structure](https://www.mindstick.com/forum/157579/what-is-a-graph-in-data-structure-explain-it-with-an-example) and aggregation needs.

**Also, Read:** [Explain the SQL Server backups and their types](https://www.mindstick.com/articles/336326/explain-the-sql-server-backups-and-their-types)

---

Original Source: https://www.mindstick.com/articles/336334/define-the-pivot-table-with-examples-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
