LeetCode 570 - Managers With at Least 5 Direct Reports (Python and SQL) [Medium]

Опубликовано: 07 Февраль 2026
на канале: Ryan & Matt Data Science
647
13

🧠 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...

In this tutorial, we walk through LeetCode Problem 570: Managers With at Least 5 Direct Reports using both SQL and Python (Pandas). This is a great exercise in grouping, counting, and filtering—key skills for interviews and real-world data work!

LeetCode Question: https://leetcode.com/problems/manager...


🚀 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
LeetCode Playlist:    • Leetcode Data Science Interview Questions  
LeetCode 586:    • LeetCode 586 Customer Placing the Largest ...  
LeetCode 595:    • LeetCode 595 - Big Countries (Python and S...  
LeetCode 619:    • LeetCode 619 -Biggest Single Number (Pytho...  


In this video, I walk through LeetCode problem 570, which asks you to find managers with at least five direct reports. This medium-difficulty problem is a great example of using self-joins in SQL and merge operations in Python pandas. I show you both solutions line by line, explaining exactly why each step is necessary.

We start with the SQL approach, where I demonstrate how to self-join the employee table to map manager IDs to manager names. I explain the group by clause and why we need to include both the manager name and manager ID to handle edge cases where multiple managers share the same name. Then I use a having clause to filter for managers with five or more reports.

Next, I tackle the same problem using Python pandas. I show how to use groupby and size to count reports, filter the results, and then merge the filtered data frame back with the original employee table to get the manager names. I also troubleshoot a common bracket error that many pandas users encounter when selecting columns.

By the end of this tutorial, you will understand self-joins in SQL, the having clause, pandas merge operations, and how to approach medium-level LeetCode SQL and pandas problems. Perfect for data analyst and data science interview prep.

TIMESTAMPS
00:00 Problem Introduction
00:40 Understanding the Example Input
01:00 SQL Solution - Self Join Setup
02:00 SQL Solution - Group By & Having Clause
02:50 SQL Code Review & Walkthrough
03:50 Python Pandas Solution - Group By & Size
04:40 Python Pandas - Filtering Results
05:10 Python Pandas - Merging DataFrames
06:20 Final Solution Review

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.