Categories: SQL Tutorials

How to Pivot Queries in SQL with Examples?

In my previous article we have explained about Pivot in SQL with examples. In SQL, pivoting refers to the process of transforming rows into columns or columns into rows. Pivoting queries can be useful when you need to present your data in a different format than what is stored in the database. This article will explain how to pivot queries in SQL with examples.

Example :

Let’s start with an example dataset. Consider a table named “Sales” that contains the following data:

DateProductSales
2022-01-01A100
2022-01-02A150
2022-01-01B200
2022-01-02B250

In this example, we have sales data for two products, A and B, for two different dates.

Pivoting Rows into Columns

To pivot rows into columns, we use the “PIVOT” operator. The “PIVOT” operator takes the values from a specified column and turns them into column headers. Here’s an example:

SELECT *
FROM Sales
PIVOT (
    SUM(Sales)
    FOR Product IN ([A], [B])
) AS P

In this query, we are pivoting the “Product” column into column headers. We are also calculating the sum of sales for each product using the “SUM” function. The resulting table looks like this:

DateAB
2022-01-01100200
2022-01-02150250

Now we have two columns, “A” and “B”, that represent the sales for each product.

Pivoting Columns into Rows

To pivot columns into rows, we use the “UNPIVOT” operator. The “UNPIVOT” operator takes the column headers and turns them into rows. Here’s an example:

SELECT *
FROM (
    SELECT Date, A, B
    FROM Sales
) AS P
UNPIVOT (
    Sales FOR Product IN (A, B)
) AS U

In this query, we are unpivoting the “A” and “B” columns into rows using the “UNPIVOT” operator. We are also selecting the “Date” column from the “Sales” table. The resulting table looks like this:

DateProductSales
2022-01-01A100
2022-01-02A150
2022-01-01B200
2022-01-02B250

Now we have three columns, “Date”, “Product”, and “Sales”, with each row representing a single sale.

Conclusion

Pivot queries in SQL can be a useful tool for transforming your data into a different format. Whether you’re pivoting rows into columns or columns into rows, the “PIVOT” and “UNPIVOT” operators can help you achieve your desired results.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Recent Posts

The Essential Guide to Cryptocurrency Exchange Platform Development for Beginners and Beyond

Introduction Cryptocurrencies took the world by storm, setting up a new financial system and breaking…

5 months ago

Top 20 System Administrator Interview Questions and answers

In my previous article I have given Top 20 technical support interview questions with its…

8 months ago

Desktop Support Scenario Based Interview Questions

In my previous articles I have given 15 most asked desktop support interview questions with…

8 months ago

How Do Business Analysts Use SQL for Their Needs?

A business analyst is someone who is versed in processes of data analysis used for…

8 months ago

Top 15 Control-M Interview Questions with Answers

In my previous article I have already given top questions and answers for Desktop support…

8 months ago

Top 20 SQL Interview Questions for Production Support

In my previous article I have given unix production support interview questions and answers. In…

8 months ago