🧠 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 are taking a look at Partition By within MS SQL. In this tutorial I go over 3 different usecase examples and show you how to code with a partition by so that you can ace a technical SQL interview.
Everything is coded within MSSQL and inside SQL Server Management Studio.
🚀 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 Merge: • SQL MERGE for Beginners: A Full Tutorial w...
SQL Self Join: • SQL Self Joins Made Easy! (3 Examples)
SQL Unpivot: • SQL Unpivot Simplified (Turn Columns into ...
Learn SQL Partition By with three practical baseball statistics examples that show you how to aggregate data without losing individual row details. In this SQL tutorial, I walk through using Partition By with COUNT, AVG, and SUM functions to analyze player stats across different teams and leagues.
We start with a simple example counting players per team, then move to calculating league batting averages using a subquery to handle rounding, and finish with summing team hits and home runs. Each example uses real baseball data from players like Mike Trout, Shohei Ohtani, Mookie Betts, and Aaron Judge to demonstrate how Partition By differs from Group By.
The key advantage of SQL Partition By is that it allows you to perform aggregate calculations while still maintaining all your original columns and rows, unlike Group By which restricts your output. I cover the essential syntax structure with the OVER clause and show you exactly when and why you would use this powerful window function in your SQL queries.
By the end of this tutorial, you'll understand how to write Partition By statements for common data analysis tasks and avoid the limitations of traditional GROUP BY aggregations. Perfect for data analysts, data scientists, and anyone working with SQL databases who needs to perform calculations across groups while preserving individual record details.
TIMESTAMPS
00:00 Introduction to SQL Partition By
00:45 Baseball Stats Table Overview
01:42 Example 1: Count Players Per Team
02:27 Example 1: Results Explained
02:45 Example 2: Average Batting Average by League
03:52 Handling Round Function with Sub-Query
05:19 Example 3: Sum Hits and Home Runs by Team
06:38 Example 3: Results Explained
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.