In this training session, Austin Libal, a trainer at Pragmatic Works, demonstrates how to pivot data in SQL to make it more understandable and consumable. Austin uses a movie dataset to walk through two different methods for pivoting data: the PIVOT operator and CASE statements. His goal is to show how SQL transformations can convert normalized data into a more user-friendly format, much like an Excel-style report.
Why Pivot Data?
Pivoting is valuable when dealing with business questions that require data to be presented in an aggregated, easy-to-read format. Instead of returning rows of results, pivoting allows values from a column to become new columns. This makes comparisons across categories simpler and more consumable for users who may not be SQL experts.
Creating a Sample Dataset
To demonstrate pivoting, Austin creates a temporary table named Movies. This table contains:
- Movie ID (primary key)
- Movie name
- Release year
- Director
- Genre
With sample data inserted, the dataset includes 10 movies across various genres. The business question Austin poses is: “How many movies in each genre were released per year?”
Method 1: Using the PIVOT Operator
Austin first demonstrates the PIVOT operator. He begins by writing a subquery that selects the release year and genre from the Movies table. This subquery serves as the source for the pivot.
Steps include:
- Wrap the subquery as a source.
- Apply the
PIVOToperator, using an aggregation function—in this case,COUNTof genres. - Define which column values should become new columns (e.g., Sci-Fi, Drama, Crime, Action, Romance).
- Use
ORDER BYto display results by release year.
The result is a dataset showing release years with counts of movies per genre. For example, in 1994 there were two drama films, while 1997 had one romance film (Titanic).
Method 2: Using CASE Statements
As an alternative, Austin shows how to achieve the same pivoted result using CASE statements with aggregation. Instead of PIVOT, this method uses conditional logic within SUM functions.
Key steps include:
- Write a
SELECTquery with release year included. - For each genre, create a
SUM(CASE WHEN Genre = 'X' THEN 1 ELSE 0 END)expression. - Group results by release year to ensure correct aggregation.
- Order results by release year.
The final output mirrors the pivot operator’s result, giving flexibility for users who may be more comfortable with traditional SQL structures like CASE.
Practical Applications
Pivoting data in SQL is especially useful when creating reports that need to be shared with non-technical users. It enables teams to quickly analyze trends and distributions across categories. Whether using PIVOT or CASE, both methods allow SQL practitioners to reshape their datasets into more actionable formats.
Learning More
Austin concludes by encouraging viewers to explore additional learning opportunities through Pragmatic Works. Their On-Demand Learning Platform offers courses dedicated to pivoting, unpivoting, and many other SQL operations. Pragmatic Works also runs SQL Boot Camps that guide learners of all levels—from beginners to seasoned professionals—through practical applications of SQL.
Don't forget to check out the Pragmatic Works' on-demand learning platform for more insightful content and training sessions on SQL and other Microsoft applications. Be sure to subscribe to the Pragmatic Works YouTube channel to stay up-to-date on the latest tips and tricks.
Sign-up now and get instant access
ABOUT THE AUTHOR
Austin is a Jacksonville native who graduated from The Baptist College of Florida in 2012. He previously worked as a manager in the retail service industry. He enjoys spending time with his wife and two kids. His primary focus at Pragmatic Works is on Azure Synapse Analytics and teaching the best practices for data integration, enterprise data warehousing, and big data analytics. He also enjoys helping customers learn the ins and outs of Power BI and showing people new ways to grow their business with the Power Platform.
Free Community Plan
On-demand learning
Most Recent
private training

Leave a comment