Master the Pivot Function in Oracle SQL: Transform Rows to Columns Efficiently

The PIVOT function in Oracle SQL is a powerful tool for data transformation. It allows you to convert rows into columns, or vice versa, without using complex joins or self-joins. By performing a conditional redistribution of your data, PIVOT can simplify complex queries and make your results more understandable. In this article, we will explore the PIVOT function, its syntax, and how to use it with practical examples.

Oracle SQL Developer: How to transpose rows to columns using PIVOT function
Oracle SQL Developer: How to transpose rows to columns using PIVOT function

Before diving into PIVOT, let's consider a scenario where traditional SQL might struggle. Imagine you have a table with sales data, structured with rows for each product and column for each year. If you want to see the total sales for each product across multiple years, you would typically need to use complex aggregated subqueries or self-joins. This is where PIVOT can simplify your query.

SQL IN NUTSHELL
SQL IN NUTSHELL

Understanding the PIVOT Function

The basic syntax of the PIVOT function is:

Oracle / PLSQL: PIVOT Clause
Oracle / PLSQL: PIVOT Clause

SELECT * FROM TABLE PIVOT (expression FOR operand IN (value1, value2, ...))

The PIVOT function takes an aggregate function, an operand (the column to pivot), and a list of values you want to convert to columns.

SQL Window Functions Explained with Examples | Beginner SQL Tutorial
SQL Window Functions Explained with Examples | Beginner SQL Tutorial

Pivoting Columns Using a Single Aggregate Function

Let's start with a simple example. Say you have a SALES table structured like this:

PRODUCT YEAR SALES

Window Functions in SQL
Window Functions in SQL

--------------------

Apple 2020 100

Orange 2020 150

a table with numbers and symbols on it that says,'s the number functions for each
a table with numbers and symbols on it that says,'s the number functions for each

Apple 2021 200

To get total sales for each product across 2020 and 2021, you can use PIVOT like this:

๐Ÿ’ผ Most Asked SQL Interview Question!  โ“ Whatโ€™s the difference between DELETE, TRUNCATE & DROP? ๐Ÿšจ One keeps structure, one resets it, one wipes it out!  ๐Ÿ‘‰Preparing for interview? Check the Bio for playlist link  ๐Ÿ‘‰ Tag a friend who needs this! ๐Ÿ”–save it for your prep!  ๐Ÿ‘‰ HackerRank Topic wise SQL practice Questions: Link in Bio  Follow for daily data insights ๐Ÿ‘‡ ๐Ÿ“ธ Instagram | โ–ถ๏ธ YouTube | ๐Ÿ“˜ Facebook  . . . . . #sqlinterview #datascience #dataanalytics #sql #interviewprep #data #database #business... Interview Prep, How To Learn Sql For Data Science, Data Analyst, Computer Coding, Data Analysis Activities, Data Science Learning, Sql Programming, Job Advice, Sql Commands
๐Ÿ’ผ Most Asked SQL Interview Question! โ“ Whatโ€™s the difference between DELETE, TRUNCATE & DROP? ๐Ÿšจ One keeps structure, one resets it, one wipes it out! ๐Ÿ‘‰Preparing for interview? Check the Bio for playlist link ๐Ÿ‘‰ Tag a friend who needs this! ๐Ÿ”–save it for your prep! ๐Ÿ‘‰ HackerRank Topic wise SQL practice Questions: Link in Bio Follow for daily data insights ๐Ÿ‘‡ ๐Ÿ“ธ Instagram | โ–ถ๏ธ YouTube | ๐Ÿ“˜ Facebook . . . . . #sqlinterview #datascience #dataanalytics #sql #interviewprep #data #database #business... Interview Prep, How To Learn Sql For Data Science, Data Analyst, Computer Coding, Data Analysis Activities, Data Science Learning, Sql Programming, Job Advice, Sql Commands
Oracle SQL Cheat Sheet
Oracle SQL Cheat Sheet
an info poster showing the different features of oracle's ql - performance software
an info poster showing the different features of oracle's ql - performance software
SQL in 60 days
SQL in 60 days
SQL Aggregate Functions Made Easy ๐Ÿ“Š | SUM, COUNT, AVG, MAX, MIN with Examples
SQL Aggregate Functions Made Easy ๐Ÿ“Š | SUM, COUNT, AVG, MAX, MIN with Examples
7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio
Dynamic SQL Pivoting - Stealing Anton's Thunder - Conclusion AMIS Technology Blog
Dynamic SQL Pivoting - Stealing Anton's Thunder - Conclusion AMIS Technology Blog
SQL
SQL
(2) Facebook Software, Engineering
(2) Facebook Software, Engineering
SQL Aggregate Functions Cheat Sheet for Beginners
SQL Aggregate Functions Cheat Sheet for Beginners
7 SQL QUERIES EVERY STUDENT SHOULD MASTER
7 SQL QUERIES EVERY STUDENT SHOULD MASTER
Formal SQL Tuning for Oracle Databases: Practical Efficiency - Efficient Practice - Hardback
Formal SQL Tuning for Oracle Databases: Practical Efficiency - Efficient Practice - Hardback
SQL ROW_NUMBER() Explained with Examples | Beginner SQL Window Function Tutorial
SQL ROW_NUMBER() Explained with Examples | Beginner SQL Window Function Tutorial
Oracle PLSQL - Difference between procedure and function
Oracle PLSQL - Difference between procedure and function
SQL RIGHT() Explained with Examples | Extract Characters from the Right in SQL Server
SQL RIGHT() Explained with Examples | Extract Characters from the Right in SQL Server
an info poster showing the different types of web pages and how they are used to create them
an info poster showing the different types of web pages and how they are used to create them
an image of a circular diagram with the names and functions
an image of a circular diagram with the names and functions
Top 20 SQL Queries for Practice | SQL Cheat Sheet for Beginners & Interviews
Top 20 SQL Queries for Practice | SQL Cheat Sheet for Beginners & Interviews

Pivoting Multiple Columns Using Multiple Aggregate Functions

Now, let's say you want to pivot multiple columns using multiple aggregate functions. For instance, you want to find the minimum, maximum, and average sales for each product in 2020 and 2021. You can achieve this by using the following query:

Unpivoting Data with the UNPIVOT Function

Oracle SQL also provides the UNPIVOT function, which is the opposite of PIVOT. Instead of converting rows into columns, UNPIVOT converts columns into rows. This can be useful when you need to rotate data from a column to a row format. For example, the following UNPIVOT query will convert the following data:

UNPIVOT Example

PRODUCT 2020 2021

Apple 100 200

Orange 150 0

Into this data:

PRODUCT YEAR SALES

-------- ----- -----

Apple 2020 100

Apple 2021 200

Orange 2020 150

Orange 2021 0

The PIVOT and UNPIVOT functions can greatly simplify complex data transformations in Oracle SQL. By understanding how to use these functions effectively, you can create more efficient and readable queries. This can lead to improved performance and reduced maintenance costs in the long run. So, the next time you're struggling with complex joins or self-joins, consider whether PIVOT or UNPIVOT can streamline your query.

Related Articles

Sql Vs Nosql W3schools Nested Sql Query W3schools W3 School Sql Statement Join Sql Query W3schools Does Not Equal Sql Server Nosql Mongodb W3schools Regular Expression In Oracle Sql Query Examples Top 100 Sql Interview Questions For Data Analyst Top 100 Sql Interview Questions Nosql Query W3schools