SQL PIVOT Tutorial 🎯

beginner
12 min

SQL PIVOT Tutorial 🎯

Welcome to this comprehensive guide on SQL PIVOT! This tutorial is designed for both beginners and intermediate learners. By the end of this lesson, you'll be confident in using the SQL PIVOT function to transform your data into a more structured format. Let's dive in!

What is SQL PIVOT? 📝

The SQL PIVOT function is a powerful tool that helps you reorganize your data from rows to columns. It's particularly useful when you want to visualize your data in a tabular format where each column represents a category, and each row represents a unique value within that category.

Why Use SQL PIVOT? 💡

Imagine you have sales data for different products across multiple regions. With SQL PIVOT, you can easily transform this data into a table where each column represents a region, and each row represents the total sales for a particular product. This structure makes it easier to analyze and compare the sales performance across regions.

The SQL PIVOT Syntax 📝

sql
SELECT Product, Region1 as Region_1, Sales1 as Sales, Region2 as Region_2, Sales2 as Sales, ... FROM Sales_Data PIVOT ( SUM(Sales) FOR Region IN (Region1, Region2, ...) ) as Pivoted_Data;

Let's break down this syntax:

  1. SELECT Product, Region1 as Region_1, Sales1 as Sales, ... - This selects the columns we want to display in the final result. The as keyword is used to rename columns for better readability.

  2. FROM Sales_Data - This specifies the table from which we're selecting the data.

  3. PIVOT - This keyword indicates that we want to transform our data.

  4. SUM(Sales) - This aggregates the sales data. In the PIVOT clause, we specify how we want to group and aggregate our data.

  5. FOR Region IN (Region1, Region2, ...) - This defines the groups (or categories) we want to create.

  6. as Pivoted_Data - This is an optional alias for the final result.

Practical Example 🎯

Let's consider a table named Sales_Data with the following structure:

sql
Product | Region | Sales --------|--------|------- Product1| Region1| 1000 Product1| Region2| 2000 Product2| Region1| 500 Product2| Region2| 3000

To pivot this data, we would write:

sql
SELECT Product, Region1 as Region_1, Sales1 as Sales_Region1, Region2 as Region_2, Sales2 as Sales_Region2 FROM Sales_Data PIVOT ( SUM(Sales) FOR Region IN (Region1, Region2) ) as Pivoted_Data;

The output would be:

Product | Region_1 | Sales_Region1 | Region_2 | Sales_Region2 --------|----------|---------------|----------|--------------- Product1| Region1 | 1000 | Region2 | 2000 Product2| Region1 | 500 | Region2 | 3000

Advanced Example 🎯

In a more complex scenario, you might have multiple categories and want to pivot them all at once. Here's an example:

sql
SELECT Product, Category1 as Category_1, Sales1 as Sales_Category1, Category2 as Category_2, Sales2 as Sales_Category2, Category3 as Category_3, Sales3 as Sales_Category3 FROM Sales_Data PIVOT ( SUM(Sales) FOR Category IN (Category1, Category2, Category3) ) as Pivoted_Data;

This would give you a result like:

Product | Category_1 | Sales_Category1 | Category_2 | Sales_Category2 | Category_3 | Sales_Category3 --------|------------|------------------|------------|------------------|------------|------------------ Product1| Category1 | 1000 | Category2 | 2000 | Category3 | NULL Product2| Category1 | 500 | Category2 | 3000 | Category3 | NULL

Quiz 🎯

Quick Quiz
Question 1 of 1

What does the SQL PIVOT function do?

With this, you've completed our SQL PIVOT tutorial! By now, you should feel comfortable using the PIVOT function to transform your data into a more manageable format. Keep practicing and remember, the more you code, the better you get! Happy coding! 🎉