Master SQL UNPIVOT 3 Columns Like a Pro: Fast Guide

The PIVOT and UNPIVOT functions in SQL are incredibly powerful when it comes to transforming data from a row-based format to a column-based format, and vice versa. Today, we're diving deep into the world of UNPIVOT, focusing on a scenario where we unpivot three columns. Let's get started!

Impress Your Coworkers by Using SQL UNPIVOT!
Impress Your Coworkers by Using SQL UNPIVOT!

Before we leap into the three-column unpivot, let's recap what UNPIVOT does. UNPIVOT rearranges data from one format (like columns) into another (like rows). It's the reverse of PIVOT, which is why we use UNPIVOT to 'unpivot' data. Now, let's tackle our three-column unpivot scenario.

SQL Pivot in all databases: MySQL, MariaDB, SQLite, PostgreSQL, Oracle, …
SQL Pivot in all databases: MySQL, MariaDB, SQLite, PostgreSQL, Oracle, …

Understanding Three-Column UNPIVOT

When unpivoting, you're transforming data from columns into rows. So, for three columns, you'll end up with three row values per data entry. Understanding this shift in data structure is key to working with UNPIVOT effectively. Let's break this down further.

SQL - Select all columns that satisfy a specific condition
SQL - Select all columns that satisfy a specific condition

The PIVOTED Data Structure

Let's assume we have a table, SalesData, with an ID, Product, and 2021Sale_Jan, 2021Sale_Feb, and 2021Sale_Mar columns, where product sales for each month are stored. The PIVOTED data structure looks like this:

>SQL : Multiple Row and Column Subqueries - w3resource
>SQL : Multiple Row and Column Subqueries - w3resource
ID Product 2021Sale_Jan 2021Sale_Feb 2021Sale_Mar
1 Apples 10 15 20

UNPIVOTING the Data

Now, we want to UNPIVOT this data. We'll end up with a result set where each row represents one product month and the sale value. Here's how the UNPIVOTED data structure looks:

SQL - Select specific columns from a table
SQL - Select specific columns from a table
ID Product Month Sale
1 Apples January 10
1 Apples February 15
1 Apples March 20

As you can see, the data is transformed from a column-based structure to a row-based structure, making it easier to analyze and query.

UNPIVOT Syntax and Examples

Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis
Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis

Now that we understand the concept, let's look at the UNPIVOT syntax and run through an example. The UNPIVOT function requires a single-column table (on which to UNPIVOT) and a multicolumn table containing the original column names. Let's dive into the syntax and see it in action.

UNPIVOT Syntax

the diagram shows how to use pivot in an application with multiple numbers and symbols
the diagram shows how to use pivot in an application with multiple numbers and symbols
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
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL in 60 days
SQL in 60 days
SQL SUM() Explained with Examples | Beginner SQL Tutorial
SQL SUM() Explained with Examples | Beginner SQL Tutorial
SQL ORDER BY clause Explained for Beginners
SQL ORDER BY clause Explained for Beginners
SQL Query Execution Order Explained | Logical Order of SQL Query Processing
SQL Query Execution Order Explained | Logical Order of SQL Query Processing
CQF - SQL
CQF - SQL
SQL queries don
SQL queries don
SQL IN NUTSHELL
SQL IN NUTSHELL
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
a blue and white poster with the names of different types of letters on it's side
a blue and white poster with the names of different types of letters on it's side
SQL Cheatsheet In 2026 🚀
SQL Cheatsheet In 2026 🚀
a blue and white poster with text on it
a blue and white poster with text on it
Top 20 SQL Queries for Practice | SQL Cheat Sheet for Beginners & Interviews
Top 20 SQL Queries for Practice | SQL Cheat Sheet for Beginners & Interviews
SQL Query Execution Order
SQL Query Execution Order
SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples
SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide

The UNPIVOT function's syntax is as follows:

  • tableColumn: Specifies the column to be unpivoted from the source table.
  • valueColumnAS: Specifies the name of the output column that holds the values from the tableColumn.
  • otherColumns: Optionally, you can add more column references from the source table.

UNPIVOT Example

Using our SalesData table, here's how we can UNPIVOT the data:

``` SELECT * FROM SalesData UNPIVOT (SaleValue FOR Month IN ([2021Sale_Jan], [2021Sale_Feb], [2021Sale_Mar])) AS unpivotedData ```

The resulting output will match our UNPIVOTED data structure described earlier.

In closing, mastering SQL's UNPIVOT function is a game-changer when working with transformed data. With UNPIVOT, you can seamlessly transform rows into columns, making your data analysis more intuitive and efficient. Keep practicing and expanding your UNPIVOT skillset to unlock new opportunities in your data-driven adventures!

Related Articles

W3school Sql Outer Join Sql Query W3schools Is Sql Easy To Use Top 100 Sql Interview Questions Sql Practice Exercises Free Not Equal To In Sql W3schools W3 Sql Select Into W3 School Sql Joins Sql With Nolock Example Sql Server Cte W3schools