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: 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
Project Team Size Analysis DashboardProject Alpha3 EmployeesTeam Size: 3Project Beta5 EmployeesTeam Size: 5 โญProject Gamma2 EmployeesTeam Size: 2Analysis ResultMaximum Team Size: 5 employeesProjects with Maximum Size: Project BetaSQL Query LogicGROUP BY project_id โ†’ COUNT employees โ†’ MAX() OVER() โ†’ WHERE count = maxWinner!
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.
Asked in
Meta 45 Amazon 38 Google 32 Microsoft 28
42.0K Views
High Frequency
~15 min Avg. Time
1.7K 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