Pivoting Data with Pivot and Unpivot
SQL (Structured Query Language) database management applications are used to store data in database tables, manage the data, extract data and information, and manipulate it as needed. Sometimes, database data is stored in row order. However, to make custom database data reports easier to understand, we need to display it in a column-order structured view. Similarly, sometimes we need to convert column-based table data back to row order.

For this purpose, database tables are managed and controlled with two essential features in SQL: PIVOT and UNPIVOT.
- PIVOT – This converts database table data from a row format to a column format view order.
- UNPIVOT – This converts database table data from a column format to a row format view order.
PIVOT and UNPIVOT are database table operations primarily used by database users to analyze existing data, create database table reports, and manage databases.
What is the PIVOT concept in SQL database management?
PIVOT is a SQL data operation method that converts unique values from rows in a database table into a column-ordered view format. It also provides numeric calculation functions like SUM, COUNT, and AVG on your table data.
PIVOT is used in SQL data operations when we want to summarize existing database information in detail and create reports in a table-formatted view of the database.
Example of PIVOT in SQL data operations.
Suppose we have a company’s sales information stored in the table below.
Company Sales Report Table
| Item | Year | Price | Qty |
| MacBook | 2027 | 170000 | 1 |
| Hp Pavilion | 2025 | 120000 | 1 |
| Dell Vostro | 2027 | 90000 | 2 |
| Asus Tuf | 2026 | 97000 | 1 |
In this database table, the details are stored in rows by year. We want to convert the years in the Sales table into columns.
In SQL Database Query (SQL Server).
SELECT *
FROM
(
SELECT Item, Year, Qty, Price
FROM Customer
) AS SourceTable
PIVOT
(
SUM(Price)
FOR Year IN ([2026], [2027])
) AS PivotTable;
The output is.
| Item | Qty | 2026 | 2027 |
| MacBook | 1 | NULL | 170000 |
| Hp Pavilion | 1 | NULL | NULL |
| Dell Vostro | 2 | NULL | 90000 |
| Asus Tuf | 1 | 97000 | NULL |
Explanation of SQL Database Query (SQL Server).
- SELECT This SQL command statement retrieves data information from the required Customer table.
- Source Table is the temporary source format of the data for pivoting.
- PIVOT converts row values in table data to columns.
- SUM(Price) calculates the total sales amount of a product item.
- FOR Year indicates the column whose values will be created as a new column.
- IN ([2026], [2027]) This indicates the name of a new database table column.
Advantages of the PIVOT feature in SQL database management:
- Easily converts SQL database table data from row-based to column-based data.
- It makes SQL database table data reports easy to read and analyze.
- Helps summarize large amounts of table database data information.
- Supports applying SQL numeric aggregate calculation functions like SUM and COUNT.
- Simple SQL database table data improves presentation and analysis.
What is UNPIVOT in SQL database management?
The UNPIVOT feature in SQL database management is the opposite database operation to the PIVOT function. It converts database table data from columns to rows.
The UNPIVOT feature in SQL is used when database data is stored in multiple columns, but this data information needs to be converted to a simple row-based format for detailed analysis or processing.
Example of UNPIVOT in SQL database management.
Let’s say we have a database table containing a company’s annual sales data.
Sales Report Table
| Item | Year | Price | Qty |
| MacBook | 2027 | 170000 | 1 |
| Hp Pavilion | 2025 | 120000 | 1 |
| Dell Vostro | 2027 | 90000 | 2 |
| Asus Tuf | 2026 | 97000 | 1 |
Here, we want to convert the Year column of this database table into rows.
UNPIVOT in SQL Database Management Query (SQL Server).
SELECT Item, Year, Price, Qty
FROM SalesTable
UNPIVOT
(
Price
FOR Year IN ([2026], [2027])
) AS UnpivotTable;
The output is.
| Item | Year | Price | Qty |
| MacBook | 2027 | 170000 | 1 |
| Dell Vostro | 2027 | 90000 | 2 |
| Asus Tuf | 2026 | 97000 | 1 |
Explanation of UNPIVOT in SQL Database Management Query.
- UNPIVOT This SQL command statement converts column data in the SalesTable table to rows.
- Price stores the values from the original column.
- FOR Year creates a new column with the original column names.
- IN ([2026], [2027]) indicates the columns in the existing database table that will later be converted to rows.
Advantages of UNPIVOT in SQL Database Management.
- The unpivot feature in SQL database tables converts column-based table data to row-based data.
- The unpivot feature makes it easier to process and analyze table data.
- It helps group information from multiple database table columns.
- It simplifies data transformation in database tables.
- SQL databases are useful for generating data table reports for reporting and detailed data analysis.
Main Difference Between PIVOT and UNPIVOT features
| # | Pivot Database Table Feature | Unpivot Database Table Feature |
| 1. | Pivot database table features used to convert rows into columns order. | Unpivot database features used to converts columns into rows format. |
| 2. | Pivot database used to detail summarize table data. | Unpivot database features used to reorganize all table data. |
| 3. | Pivot features make database table data with wider. | Unpivot database features used to makes data longer wider view. |
| 4. | Pivot features often use to apply aggregate functions in table. | Unpivot database features does not require or allow data aggregation. |
| 5. | Pivot features useful for detail database table report generation data. | Unpivot database features useful for data processing method. |
| 6. | Pivot features used to change vertical data into horizontal format view. | Unpivot database features used to changes horizontal data into vertical table format. |
Real-life uses of PIVOT and UNPIVOT in SQL database tables.
In banking systems.
- The PIVOT feature in a SQL database table allows a database user to display monthly customer account deposits in multiple columns.
- The UNPIVOT feature in a SQL database table can convert a bank customer’s monthly deposit column into separate records.
In education systems.
- The PIVOT database feature in an education system can display a student’s marks in different subjects in multiple columns.
- The UNPIVOT database feature can convert a student’s subject column into separate student-subject records.
In business commercial activity.
- The PIVOT database feature in the business industry can display annual sales statement data information for different products.
- The UNPIVOT database feature can convert the Yearly Sales column to rows for future business data analysis.
In healthcare systems.
- The PIVOT database feature can display patient test results in separate columns.
- The UNPIVOT database feature can convert patient test result columns to rows.
Benefits of using PIVOT and UNPIVOT together in SQL database tables.
- It helps convert database table data into different formats.
- Makes reporting summaries in SQL database tables more flexible.
- A better SQL database helps in detailed analysis of data in tables.
- Complex SQL database tables simplify database operations.
- It helps in preparing data for business intelligence tools.
- It improves the presentation and organization of database information.
Important things to remember about PIVOT and UNPIVOT in SQL database tables.
- PIVOT and UNPIVOT in SQL database tables are directly supported in SQL Server and some other database system software. However, the syntax for the PIVOT and UNPIVOT features may vary across database platforms.
- The PIVOT feature in SQL database tables typically requires an aggregate function such as SUM, COUNT, or AVG.
- The UNPIVOT feature reverses column-to-row transformations in SQL database tables.
- The UNPIVOT feature in SQL Server applications does not display NULL values from table columns that are being unpivoted.
- The original data must be structured in the proper order before applying these PIVOT and UNPIVOT operations to SQL database tables.
Conclusion of Pivoting Data with Pivot and Unpivot.
PIVOT and UNPIVOT are two essential SQL task operations used to transform and organize information in a database table. The PIVOT database feature converts table data from rows to columns, helping to create a detailed summarized report, while the UNPIVOT feature converts database table columns to rows, making data easier to process. The PIVOT and UNPIVOT operations in SQL database tables are useful features in banking, education, business, healthcare, and other fields. PIVOT and UNPIVOT improve data presentation, simplify detailed database analysis, and make reporting on database table data more flexible and efficient. Therefore, PIVOT and UNPIVOT play an important role in advanced SQL data management and reporting.
