SQL Self Joins Made Easy! (3 Examples)
🧠 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-automations-4579 Today we are taking a look at 3 different examples of self joins that use either left or inner joins Everything is coded within MSSQL and inside SQL Server Management Studio. 🚀 Hire me for Data Work: https://ryanandmattdatascience.com/data-freelancing/ 👨💻 Mentorships: https://ryanandmattdatascience.com/mentorship/ 📧 Email: [email protected] 🌐 Website & Blog: https://ryanandmattdatascience.com/ 🖥️ Discord: https://discord.com/invite/F7dxbvHUhg 📚 *Practice SQL & Python Interview Questions: https://stratascratch.com/?via=ryan 📖 *SQL and Python Courses: https://datacamp.pxf.io/XYD7Qg 🍿 WATCH NEXT SQL Tutorials Playlist: https://www.youtube.com/playlist?list=PLcQVY5V2UY4KvkR1ogGVh41xmj5ENnjLs SQL Pivots: https://youtu.be/Lw9kRlNai6Y SQL Datediff: https://youtu.be/RYphaulfsfc SQL Case Statements: https://youtu.be/i84SxoBCz6U In this video, I break down SQL self joins through three practical examples that you'll encounter in data analyst interviews and daily work. Self joins allow you to join a table to itself, which is incredibly powerful for comparing rows within the same dataset. I start by explaining the fundamental concept of self joins—how you can select from Table A and join it back to itself to extract and compare data. Then we dive into three hands-on examples: finding consecutive numbers in a sequence, mapping parent-child relationships, and comparing career statistics across different years. The first example shows you how to identify consecutive duplicate numbers using inner joins. The second demonstrates parent-child hierarchies with left joins to preserve all records, even those with null values. The third example gets into sports analytics, comparing baseball stats across seasons where win totals match. Each example includes complete SQL code, table aliases, join conditions, and filtering techniques. By the end of this tutorial, you'll understand when to use inner joins versus left joins in self join scenarios, how to structure your join conditions properly, and how to avoid common pitfalls like accidentally joining a row to itself. Whether you're preparing for data analyst interviews on platforms like LeetCode or need to solve real business problems at work, these self join patterns will level up your SQL skills significantly. TIMESTAMPS 00:00 Introduction to Self Joins 00:38 What is a Self Join? 01:11 Example 1: Consecutive Numbers 03:27 Building the Self Join Query 04:14 Example 2: Parent-Child Relationships 05:45 Selecting Parent Names 07:24 Example 3: Comparing Career Stats 09:01 Analyzing Multiple Seasons OTHER SOCIALS: Ryan’s LinkedIn: https://www.linkedin.com/in/ryan-p-nolan/ Matt’s LinkedIn: https://www.linkedin.com/in/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.
Download
1 formatsVideo Formats
Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.