Back to Browse

LeetCode 176 – Second Highest Salary (Python and SQL) [MEDIUM]

665 views
Mar 27, 2024
10:21

🧠 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 tutorial, we solve LeetCode Problem 176 – Second Highest Salary using both SQL and Python (Pandas). Learn multiple techniques to handle ranking problems, deal with NULL results, and think like a database pro! Code: https://ryanandmattdatascience.com/leetcode-176/ 🚀 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 262: https://youtu.be/VnTXSYLIS14 LeetCode 184: https://youtu.be/s4e_OA6FZVY LeetCode 175: https://youtu.be/l4NzniB-rj4 In this video, I walk through solving LeetCode problem 176 - finding the second highest salary from an employee table. This is a medium difficulty problem that requires handling multiple edge cases, including when there's no second highest salary or when duplicate salaries exist. I provide complete solutions in both SQL and Python Pandas, breaking down each approach step by step so you can understand the logic behind every line of code. For the SQL solution, I demonstrate how to use a CTE (Common Table Expression) combined with DENSE_RANK to properly handle salary rankings and duplicates. I explain why DENSE_RANK is the right choice over other ranking functions and show how to return NULL when no second highest salary exists. The Python Pandas solution covers dropping duplicates, implementing conditional logic with if statements, and using the rank function with proper DataFrame manipulation. I also explain why certain edge cases need special handling and how both solutions address them effectively. By the end of this tutorial, you'll understand how to approach ranking problems in SQL and Pandas, handle NULL values correctly, and write clean, efficient code for similar LeetCode questions. If you're preparing for data analyst or data engineer interviews, this video will help you master essential SQL and Python techniques. TIMESTAMPS 00:00 Problem Introduction 00:31 Understanding the Problem & Edge Cases 01:12 SQL Solution - Building the CTE 02:27 SQL Solution - Dense Rank Explanation 02:58 SQL Solution - Final Query & Testing 04:22 Python Pandas Solution - Dropping Duplicates 05:50 Python Pandas Solution - Handling Null Cases 07:07 Python Pandas Solution - Ranking & Output 09:19 Solution Review & Recap 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 formats

Video Formats

360pmp417.1 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

LeetCode 176 – Second Highest Salary (Python and SQL) [MEDIUM] | NatokHD