Project Employees II - Problem
Project Employees II is a SQL database problem that challenges you to find projects with the maximum number of employees. You're given two related tables:
The Project table contains project-employee assignments with columns
Your task is to identify which projects have the highest employee count. If multiple projects tie for the maximum number of employees, return all of them. This problem tests your ability to work with aggregate functions, subqueries, and GROUP BY clauses in SQL.
Project and Employee.The Project table contains project-employee assignments with columns
project_id and employee_id. The Employee table contains employee details including employee_id, name, and experience_years.Your task is to identify which projects have the highest employee count. If multiple projects tie for the maximum number of employees, return all of them. This problem tests your ability to work with aggregate functions, subqueries, and GROUP BY clauses in SQL.
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 | Khaled | 3 |
| 2 | Ali | 2 |
| 3 | John | 1 |
| 4 | Doe | 2 |
+-------------+--------+------------------+
โบ
Output:
+-------------+
| project_id |
+-------------+
| 1 |
+-------------+
๐ก Note:
Project 1 has 3 employees (1, 2, 3) while Project 2 has 2 employees (1, 4). Since 3 > 2, Project 1 has the most employees.
example_2.sql โ Tie Case
$
Input:
Project table:
+-------------+-------------+
| project_id | employee_id |
+-------------+-------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 2 | 4 |
| 3 | 5 |
+-------------+-------------+
Employee table:
+-------------+--------+------------------+
| employee_id | name | experience_years |
+-------------+--------+------------------+
| 1 | Alice | 5 |
| 2 | Bob | 3 |
| 3 | Carol | 4 |
| 4 | Dave | 2 |
| 5 | Eve | 1 |
+-------------+--------+------------------+
โบ
Output:
+-------------+
| project_id |
+-------------+
| 1 |
| 2 |
+-------------+
๐ก Note:
Both Project 1 and Project 2 have 2 employees each, which is the maximum. Project 3 has only 1 employee. Return both projects with maximum employees.
example_3.sql โ Single Project
$
Input:
Project table:
+-------------+-------------+
| project_id | employee_id |
+-------------+-------------+
| 1 | 1 |
+-------------+-------------+
Employee table:
+-------------+--------+------------------+
| employee_id | name | experience_years |
+-------------+--------+------------------+
| 1 | John | 5 |
+-------------+--------+------------------+
โบ
Output:
+-------------+
| project_id |
+-------------+
| 1 |
+-------------+
๐ก Note:
Only one project exists with one employee. It has the maximum (and only) employee count, so return Project 1.
Constraints
- 1 โค Project.project_id โค 103
- 1 โค Project.employee_id โค 104
- 1 โค Employee.employee_id โค 104
- 1 โค Employee.name.length โค 20
- 1 โค Employee.experience_years โค 20
- All employee_id values are unique in Employee table
- Each (project_id, employee_id) pair is unique in Project table
Visualization
Tap to expand
Understanding the Visualization
1
Count Team Members
For each project, count how many employees are assigned
2
Find Maximum Size
Determine what the largest team size is across all projects
3
Identify Top Projects
Return all projects that have the maximum team size
Key Takeaway
๐ฏ Key Insight: Use window functions like MAX() OVER() to calculate global statistics while grouping data, enabling efficient single-pass solutions for finding records with maximum/minimum values.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code