LeetCode 185 - Department Top Three Salaries (Python and SQL) [Hard]
🧠 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 In this advanced-level tutorial, we solve LeetCode 185: Department Top Three Salaries using both SQL and Python (Pandas). This is a classic SQL interview problem focused on ranking, partitioning, and extracting the top-N results per group—essential skills for any serious data analyst or engineer. 🚀 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 LeetCode Playlist: https://www.youtube.com/playlist?list=PLcQVY5V2UY4J_wlwSXiEuf2KrEBan0Ysv LeetCode 607: https://youtu.be/l_SnJzz1IgM LeetCode 595: https://youtu.be/W4swQ3A4Djc LeetCode 196: https://youtu.be/VNTKXnT2_eE In this video, I break down LeetCode 185, "Department Top Three Salaries," showing you how to solve it using both SQL and Python pandas. This is labeled as a hard problem, but I walk you through why it's actually more medium-level difficulty when you understand the right approach. We start by analyzing the problem: finding employees who earn in the top three unique salaries within their department. The key insight is choosing the right ranking function—dense_rank in SQL and the dense method in pandas—to handle tied salaries correctly. I explain exactly why dense_rank allows us to go from rank 1 to 2, 2, then 3, which is essential for this problem. On the SQL side, I demonstrate my preferred approach using a CTE (Common Table Expression) to make the solution clean and easy to filter. We join the employee and department tables, partition by department ID, order by salary descending, and then filter where the salary rank is less than four. I explain each step clearly so you can apply this pattern to similar ranking problems. For the Python pandas solution, I show you how to merge dataframes, use the groupby method with rank, and apply the dense method with ascending=False. I also cover a quick column renaming technique that keeps your code concise. By the end, you'll have two complete, accepted solutions and understand exactly when to use dense ranking versus other ranking methods in both SQL and Python pandas. TIMESTAMPS 00:00 Problem Introduction & Overview 00:51 Understanding the Example & Requirements 01:42 SQL Solution - Setting Up CTE 02:23 Building the Join Query 03:02 Adding Dense Rank Function 04:03 Filtering Top Three Salaries 05:34 Python Pandas Solution - Merging DataFrames 06:52 Creating Salary Rankings in Pandas 07:52 Filtering and Formatting Results 09:19 Final Solution Review 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.