Project Employees III - Problem
Project Employees III - Finding the Most Experienced Team Members

You are a project manager at a tech company, and you need to identify the most experienced employees working on each project. This is crucial for making important technical decisions and assigning leadership roles.

Problem: Given two database tables - Project (which maps employees to projects) and Employee (which contains employee details including experience), write a SQL query to find the most experienced employee(s) in each project.

Key Details:
• If multiple employees have the same maximum experience years in a project, return all of them
• The result should show project_id, employee_id, name, and experience_years
• Return results in any order

This problem tests your ability to work with window functions, joins, and ranking in SQL - essential skills for database management and analytics roles.

Input & Output

example_1.sql — Basic Case
$ Input: Project table: | project_id | employee_id | |------------|-------------| | 1 | 1 | | 1 | 2 | | 1 | 3 | | 2 | 1 | | 2 | 4 | Employee table: | employee_id | name | experience_years | |-------------|-------|------------------| | 1 | Alice | 12 | | 2 | Bob | 7 | | 3 | Carol | 10 | | 4 | Dave | 12 |
Output: | project_id | employee_id | name | experience_years | |------------|-------------|-------|------------------| | 1 | 1 | Alice | 12 | | 2 | 1 | Alice | 12 | | 2 | 4 | Dave | 12 |
💡 Note: In project 1, Alice has the maximum experience (12 years). In project 2, both Alice and Dave have maximum experience (12 years), so both are returned.
example_2.sql — Multiple Ties
$ Input: Project table: | project_id | employee_id | |------------|-------------| | 1 | 1 | | 1 | 2 | | 1 | 3 | Employee table: | employee_id | name | experience_years | |-------------|---------|------------------| | 1 | Alice | 10 | | 2 | Bob | 10 | | 3 | Charlie | 10 |
Output: | project_id | employee_id | name | experience_years | |------------|-------------|---------|------------------| | 1 | 1 | Alice | 10 | | 1 | 2 | Bob | 10 | | 1 | 3 | Charlie | 10 |
💡 Note: All three employees have the same maximum experience (10 years) in project 1, so all are returned.
example_3.sql — Single Employee per Project
$ Input: Project table: | project_id | employee_id | |------------|-------------| | 1 | 1 | | 2 | 2 | | 3 | 3 | Employee table: | employee_id | name | experience_years | |-------------|-------|------------------| | 1 | Alice | 5 | | 2 | Bob | 8 | | 3 | Carol | 12 |
Output: | project_id | employee_id | name | experience_years | |------------|-------------|-------|------------------| | 1 | 1 | Alice | 5 | | 2 | 2 | Bob | 8 | | 3 | 3 | Carol | 12 |
💡 Note: Each project has only one employee, so each employee is automatically the most experienced in their respective project.

Constraints

  • 1 ≤ project_id, employee_id ≤ 104
  • employee_id in Project table exists in Employee table (foreign key constraint)
  • 1 ≤ experience_years ≤ 50
  • 1 ≤ name.length ≤ 20
  • Each (project_id, employee_id) combination in Project table is unique

Visualization

Tap to expand
🏆 Hackathon Leadership Selection ProcessHackathon A1Alice (12y)2Bob (10y)3Carol (8y)Hackathon B1Dave (15y)1Eve (15y)3Frank (9y)Hackathon C1Grace (20y)2Henry (7y)🥇 Selected Team LeadersAlice (Hackathon A), Dave & Eve (Hackathon B), Grace (Hackathon C)✨ Notice: Ties are handled naturally - both Dave & Eve become co-leaders
Understanding the Visualization
1
Gather Participants
Collect all hackathon-participant pairs with their skill levels
2
Group by Hackathon
Organize participants into their respective hackathons
3
Rank by Skill
Within each hackathon, rank participants by experience level
4
Select Leaders
Choose all rank-1 participants as team leaders (handles ties naturally)
Key Takeaway
🎯 Key Insight: Window functions with PARTITION BY allow us to rank employees within each project independently, making it easy to handle ties and find all most experienced employees in a single, efficient query.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28 Apple 22
52.3K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen