Master SQL Pivot with W3Schools Example Tutorial

SQL, or Structured Query Language, is a standard language for managing and manipulating relational databases. One of its powerful features is the ability to transpose data using a technique known as pivoting. SQL Server's PIVOT operator and Syrie's CROSS APPLY or APPLY can transform rows into columns, providing a more friendly and readable format for data analysis. Let's explore SQL pivot with an example from W3Schools.

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

W3Schools, a popular website for learning web development, offers an interactive tutorial on SQL pivot. In this tutorial, we'll transform a table containing sales data for different cities into a compact format using the PIVOT operator.

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

Before Pivot: sales data in rows

Consider the following sample **sales** table:

Impress Your Coworkers by Using SQL UNPIVOT!
Impress Your Coworkers by Using SQL UNPIVOT!
City Country Sales
Seattle USA 150000
Rio Brazil 200000
Seattle USA 250000
Cairo Egypt 100000
Rio Brazil 150000

Before pivoting, the data is displayed with each row representing a sale in a specific city.

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

Pivoting cities into columns

Now let's transform the data to have cities in columns. Assume we want to sum sales by city. Here's the SQL code using the PIVOT operator:

```sql SELECT * FROM ( SELECT City, SUM(Sales) as TotalSales FROM sales GROUP BY City ) AS SourceTable PIVOT ( SUM(TotalSales) FOR City IN ([Seattle], [Rio], [Cairo]) ) AS PivotTable; ```

This code first groups the data by city and sums the sales. Then, it pivots this data into a table with cities as column headers.

SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners

Results: sales data in columns

Here's the resulting table after the pivot operation:

Country Seattle Rio Cairo
USA 400000
Brazil 350000
Egypt 100000
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, each column represents a city, with total sales aggregated for that city.

Transforming data with SQL Server's PIVOT: an extension

SQL LIMIT Explained with Examples | Beginner SQL Tutorial
SQL LIMIT Explained with Examples | Beginner SQL Tutorial
SQL in 60 days
SQL in 60 days
7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL QUERIES EVERY STUDENT SHOULD MASTER
7 SQL QUERIES EVERY STUDENT SHOULD MASTER
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
SQL LEFT JOIN Explained with Examples | Beginner SQL Tutorial
SQL LEFT JOIN Explained with Examples | Beginner SQL Tutorial
SQL IN NUTSHELL
SQL IN NUTSHELL
SQL Window Functions Explained with Examples | Beginner SQL Tutorial
SQL Window Functions Explained with Examples | Beginner SQL Tutorial
SQL LIKE Explained with Examples | SQL Wildcards for Beginners
SQL LIKE Explained with Examples | SQL Wildcards for Beginners
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL LAG() Explained with Examples | Beginner SQL Window Function Tutorial
SQL LAG() Explained with Examples | Beginner SQL Window Function Tutorial
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
SQL LEFT() Explained with Examples | Extract Characters from the Left in SQL Server
SQL LEFT() Explained with Examples | Extract Characters from the Left in SQL Server
SQL LEAD() Explained with Examples | Beginner SQL Window Function Tutorial
SQL LEAD() Explained with Examples | Beginner SQL Window Function Tutorial
SQL GROUP BY Explained Visually (Beginner-Friendly Guide)
SQL GROUP BY Explained Visually (Beginner-Friendly Guide)
SQL RIGHT JOIN Explained with Examples | Beginner SQL Tutorial
SQL RIGHT JOIN Explained with Examples | Beginner SQL Tutorial
SQL ORDER BY clause Explained for Beginners
SQL ORDER BY clause Explained for Beginners
SQL Joins Explained with Examples | Beginner SQL Guide
SQL Joins Explained with Examples | Beginner SQL Guide
SQL JOINS Explained for Behinners
SQL JOINS Explained for Behinners

SQL Server's PIVOT operator is not available in all SQL flavors. In cases where PIVOT is not supported, such as MySQL, we can use the CROSS APPLY or APPLY operator with some creative query writing. However, this tutorial mainly focuses on SQL Server's PIVOT operator as demonstrated in the W3Schools example.

Understanding SQL pivot is essential for data professionals, enabling them to create clearer and more informative reports. By transforming data from rows to columns, pivoting offers a more intuitive and user-friendly data representation, especially when dealing with large datasets or complex queries.

Start exploring SQL pivot today and reshape your data to unlock new insights! Don't forget to bookmark W3Schools' SQL Pivot tutorial for handy reference and more practice examples.

Related Articles

Sql W3 Where Stored Procedure In Postgresql W3schools W3schools Oracle Pl Sql W3 School Sql Foreign Key 100 Sql Interview Questions Sql W3 Between Sql W3 Cast Sql Database W3 W3 School Sql Primary Key Window Functions In Sql W3