LeetCode 185 - Department Top Three Salaries (Python and SQL) [Hard]

Опубликовано: 01 Март 2026
на канале: Ryan & Matt Data Science
398
10

🧠 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 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/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 607:    • LeetCode 607 - Sales Person (Python and SQ...  
LeetCode 595:    • LeetCode 595 - Big Countries (Python and S...  
LeetCode 196:    • LeetCode 196 - Delete Duplicate Emails (Py...  

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:   / 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.