SQL PIVOTS Made Easy (Rows to Columns)

Опубликовано: 01 Март 2026
на канале: Ryan & Matt Data Science
5,257
58

🧠 Don’t miss out! Get FREE access to my Skool community — packed with resources, tools, and support to help you with Data, Machine Learning, and AI Automations! 📈 https://www.skool.com/data-and-ai-aut...

Today we take a look at how SQL pivots can turn rows into columns


🚀 Hire me for Data Work: https://ryanandmattdatascience.com/da...
👨‍💻 Mentorships: https://ryanandmattdatascience.com/me...
📧 Email: [email protected]
🌐 Website & Blog: https://ryanandmattdatascience.com/
🖥️ Discord:   / discord  
📚 *Practice SQL & Python Interview Questions: https://stratascratch.com/?via=ryan
📖 *SQL and Python Courses: https://datacamp.pxf.io/XYD7Qg

🍿 WATCH NEXT
SQL Tutorials Playlist:    • SQL Tutorials for Data Analysts  
SQL Unpivot:    • SQL Unpivot Simplified (Turn Columns into ...  
SQL String Functions:    • 12 Essential SQL String Functions You Must...  
SQL Lead and Lag functions:    • Lead & Lag Window Functions in SQL (EXAMPL...  


In this SQL tutorial, I walk you through how to use pivot tables to transform rows into columns in SQL. Pivot tables let you visualize data in a completely different way, making it much easier to analyze patterns and summarize information without complex GROUP BY statements.

We start with a basic store orders table and transform it using SQL pivot syntax, showing how to aggregate quantities across different store names. I explain the complete pivot structure step by step, including how to set up a subquery, define your aggregation functions (like SUM or COUNT), and specify which columns you want to pivot on.

I also cover practical tips like handling NULL values, ordering your columns, and cleaning up your output. By the end of this video, you'll understand exactly when to use pivot tables and how to write the syntax correctly for your own projects.

This is a more advanced SQL concept, so you should be comfortable with subqueries before diving in. The code can look complex at first, but I break down each piece so you can follow along easily. Whether you're doing business analysis, creating reports, or just want cleaner data views, mastering SQL pivots will make your queries more powerful and your results easier to read.

TIMESTAMPS
00:00 Introduction to SQL Pivots
00:42 Understanding the Data Structure
01:34 Building the Base Query
02:18 Creating the Sub Query
02:56 Writing the PIVOT Statement
04:27 Comparing Pivot vs Traditional Query
05:43 Alternative Grouping Approach
06:39 Customizing Pivot Columns
07:21 Handling NULL Values
08:51 Pivot Syntax Breakdown
09:46 Alternative Aggregate Functions

OTHER SOCIALS:
Ryan’s LinkedIn:   / ryan-p-nolan  
Matt’s LinkedIn:   / matt-payne-ceo  
Twitter/X: https://x.com/RyanMattDS

Who is Ryan
Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Who is Matt
Matt is the founder of Width.ai, an AI and Machine Learning agency. Before starting his own company, he was a Machine Learning Engineer at Capital One.

*This is an affiliate program. We receive a small portion of the final sale at no extra cost to you.