Learn how to use Pivot in SQL and Unpivot in SQL. Pivot and Unpivot in SQL are two relational operators that are used to convert a table expression into another. Pivot in SQL is used when we want to transfer data from the row level to the column level and Unpivot in SQL is used when we want to convert data from the column level to the row level. PIVOT and UNPIVOT relational operators are used to generate multidimensional reporting.
In this article, we will discuss both Pivot and Unpivot operators in a SQL Server database and how to create interactive tables that quickly combine and compares a large amount of data.
Firstly, we will create a table and insert some data into the table. You may use your own database tables.
Here is the Employee table query.
CREATE TABLE Employee
(
Name [nvarchar](max),
[Year] [int] ,
Sales [int]
)
Insert the following data into the table.
INSERT INTO Employee
SELECT 'Pankaj',2010,72500 UNION ALL
SELECT 'Rahul',2010,60500 UNION ALL
SELECT 'Sandeep',2010,52000 UNION ALL
SELECT 'Pankaj',2011,45000 UNION ALL
SELECT 'Sandeep',2011,82500 UNION ALL
SELECT 'Rahul',2011,35600 UNION ALL
SELECT 'Pankaj',2012,32500 UNION ALL
SELECT 'Pankaj',2010,20500 UNION ALL
SELECT 'Rahul',2011,200500 UNION ALL
SELECT 'Sandeep',2010,32000
Now we check the data of the Employee table.
Output

We use the above Employee table for PIVOT and UNPIVOT relational operator examples.
PIVOT in SQL
PIVOT relational operator converts data from row level to column level. PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output. Using the PIVOT operator, we can perform an aggregate operations where we need them.
Syntax
SELECT <non-pivoted column>,
<list of pivoted column>
FROM
(<SELECT query to produces the data>)
AS <alias name>
PIVOT
(
<aggregation function>(<column name>)
FOR
[<column name that become column headers>]
IN ( [list of pivoted columns])
) AS <alias name for pivot table>
Let us take some examples.
Example 1
SELECT [Year], Pankaj,Rahul,Sandeep FROM
(SELECT Name, [Year] , Sales FROM Employee )Tab1
PIVOT
(
SUM(Sales) FOR Name IN (Pankaj,Rahul,Sandeep)) AS Tab2
ORDER BY [Tab2].[Year]
Output

In the above query, we calculated the sum of sales for Pankaj, Rahul, and Sandeep employees corresponding to the year values.
Example 2
Here is another example:
SELECT Name, 2010,2011,2012 FROM
(SELECT Name, [Year] , Sales FROM Employee )Tab1
PIVOT
(
SUM(Sales) FOR [Year] IN (2010,2011,2012)) AS Tab2
ORDER BY Tab2.Name
Output

When we execute the above query, SQL Server throws an error because we can’t provide an integer value as a column name directly. To remove this error use the brackets before each integer value as in the following code snippet:
SELECT Name, [2010],[2011],[2012] FROM
(SELECT Name, [Year] , Sales FROM Employee )Tab1
PIVOT
(
SUM(Sales) FOR [Year] IN ([2010],[2011],[2012])) AS Tab2
ORDER BY Tab2.Name
Output

Example 3
In the previous examples, we wrote the name of pivot columns. This approach is useful if we know all possible values for pivot columns. But what if the number of columns changes in the database?
Let us take the previous example. In the previous example, we used 2010, 2011, and 2012 as pivot columns. But it is not fixed that this column will not change in the future. What happens when we get data from the year 2013? 2014? or even more?
To solve this problem, we need to use dynamic queries.
First, we retrieve all unique values from a pivot column and after that, we will write a dynamic query to execute it with a pivot query at run time.
Now we can execute example 2, but using the dynamic query.








TomasPosted Nov 12, 2024, 8:13 PM
Great explanation on both PIVOT and UNPIVOT in SQL Server! For those interested in going deeper into pivot transformations for data reporting, I recently covered additional techniques and best practices in my blog post here: https://finalexception.com/how-to-pivot-data-in-sql-server-for-efficient-data-reporting/
Vineet KumarPosted Jun 7, 2022, 3:31 AM
Great stuff on Pivot.
gk gkPosted Oct 22, 2021, 8:55 AM
Why tab used here
Ian KodePosted Oct 15, 2020, 5:04 PM
If you really knew TSQL you would not use multiple UNION clauses but multiple VALUES('Dick', 2020, 123),('Joe',2019,456) etc !
Dipa MehtaPosted Mar 7, 2020, 7:36 AM
Interesting stuff on a pivot with SQL Server.
Arvind KumarPosted Jun 15, 2019, 2:23 AM
Thanks Dude
Hein HtunPosted Mar 6, 2019, 9:20 PM
Thanks, bro. It helps me a lot.
jay singhPosted May 15, 2018, 7:00 AM
How to implement join in dynamic query plz help
Haribansh Kumar AgrawalPosted Jan 25, 2018, 5:06 AM
Nice article. never thought Pivoting was so Easy
Santhakumar MunuswamyPosted Sep 17, 2015, 10:45 AM
Thanks for nice article:)
Pankaj Kumar ChoudharyPosted Sep 14, 2015, 9:50 PM
Thanks Humayun Kabir Mamun Sir..........
Pankaj Kumar ChoudharyPosted Sep 14, 2015, 9:49 PM
Thanks Yashwant Vishwakarma Sir..........
Pankaj Kumar ChoudharyPosted Sep 14, 2015, 9:49 PM
Thanks Ajeet sir.........
Pankaj Kumar ChoudharyPosted Sep 14, 2015, 9:49 PM
Thanks Raja Prs
Pankaj Kumar ChoudharyPosted Sep 14, 2015, 9:48 PM
Thanks Harshad Pansuriya Sir............
Pankaj Kumar ChoudharyPosted Sep 14, 2015, 9:48 PM
Thanks Saineshwar Bageri Sir...........
Pankaj Kumar ChoudharyPosted Sep 14, 2015, 9:48 PM
Thanks Karthikeyan K Sir......... .
Pankaj Kumar ChoudharyPosted Sep 14, 2015, 9:47 PM
Thanks Shashangka Sir.......
Humayun Kabir MamunPosted Sep 14, 2015, 8:37 AM
Nice...
Yashwant VishwakarmaPosted Sep 14, 2015, 7:00 AM
Nice information !!!
Ajeet MishraPosted Sep 14, 2015, 3:48 AM
nice
Raja TPosted Sep 14, 2015, 12:28 AM
Very Nice Thanks For sharing
Harshad PansuriyaPosted Sep 14, 2015, 12:27 AM
Nice One
Saineshwar BageriPosted Sep 13, 2015, 11:51 PM
Good One
Karthikeyan KPosted Sep 13, 2015, 12:49 PM
Good one bro :) Thanks for sharing
Shashangka ShekharPosted Sep 13, 2015, 11:59 AM
Nice Share.
Pankaj Kumar ChoudharyPosted Sep 13, 2015, 11:06 AM
Thanks Upendra Pratap Shahi Sir .............
Pankaj Kumar ChoudharyPosted Sep 13, 2015, 11:03 AM
Thanks manas............
Pankaj Kumar ChoudharyPosted Sep 13, 2015, 11:03 AM
Thanks Rajeesh Menoth Sir.........
Pankaj Kumar ChoudharyPosted Sep 13, 2015, 11:03 AM
Thanks lalit kumar Sir..........
Upendra Pratap ShahiPosted Sep 13, 2015, 9:03 AM
nice article pankaj......Shandar Jabrdast Jindabad...
Manas MohapatraPosted Sep 13, 2015, 6:59 AM
Nice Share..
Rajeesh MenothPosted Sep 13, 2015, 6:54 AM
Nice one
Lalit KumarPosted Sep 13, 2015, 4:10 AM
Nice post.