Skip to main content

Project Employees II - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase4 min readAsked at: Meta
Practice this problem

Problem Statement

Table: Project

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| project_id  | int     |
| employee_id | int     |
+-------------+---------+
(project_id, employee_id) is the primary key (combination of columns with unique values) of this table.
employee_id is a foreign key (reference column) to Employee table.
Each row of this table indicates that the employee with employee_id is working on the project with project_id.

 

Table: Employee

+------------------+---------+
| Column Name      | Type    |
+------------------+---------+
| employee_id      | int     |
| name             | varchar |
| experience_years | int     |
+------------------+---------+
employee_id is the primary key (column with unique values) of this table.
Each row of this table contains information about one employee.

 

Write a solution to report all the projects that have the most employees.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

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           |
+-------------+
Explanation: The first project has 3 employees while the second one has 2.

Approach Overview

Problem Overview: The table Project(project_id, employee_id) stores which employees are assigned to each project. The task is to return the project_id that has the highest number of employees assigned. If multiple projects share the same maximum employee count, all of them should be returned.

Approach 1: GROUP BY with ORDER BY and LIMIT (O(n) time, O(k) space)

The most straightforward way is to aggregate employee counts per project using GROUP BY project_id. Once grouped, apply COUNT(employee_id) to compute the number of employees in each project. Then sort the results using ORDER BY COUNT(employee_id) DESC and select the top result with LIMIT 1. The query scans all rows once to build the grouped counts, which takes O(n) time where n is the number of rows in the table. The database stores aggregated groups internally, requiring O(k) space where k is the number of unique projects. This approach works well when you only need one project with the highest count, but it does not correctly return multiple projects if there is a tie.

Approach 2: GROUP BY with MAX Subquery (O(n) time, O(k) space)

A more robust approach calculates employee counts per project and then filters projects whose counts match the global maximum. First compute COUNT(employee_id) grouped by project_id. Then use a subquery to determine the maximum employee count among those groups. Finally, use HAVING to return only the projects whose counts equal that maximum value. The database still performs a full scan of the table and aggregates rows, so the time complexity remains O(n). Space complexity is O(k) for storing grouped counts. This method correctly handles ties because every project matching the maximum count is returned.

Both approaches rely on SQL aggregation concepts such as GROUP BY, COUNT, and filtering grouped results using HAVING. Understanding how relational databases execute aggregation queries is key when solving SQL and database interview problems. Many similar problems use the same pattern: compute grouped statistics first, then filter using a subquery or window function.

Recommended for interviews: The GROUP BY with MAX subquery approach is the safer and more correct solution because it handles ties without relying on ordering tricks. Interviewers typically expect you to recognize the aggregation pattern, compute per-group counts, and compare them against the global maximum using HAVING. The ORDER BY ... LIMIT approach demonstrates understanding of aggregation but may fail when multiple projects share the same highest employee count.

Solution

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
GROUP BY with ORDER BY LIMITO(n)O(k)Quick solution when only one maximum result is required and ties are not a concern
GROUP BY with MAX SubqueryO(n)O(k)Best general solution when multiple projects may share the highest employee count

Video Solution

LeetCode 1076 "Project Employees II" Meta Interview SQL Question with Explanation • Everyday Data Science • 2,101 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Project Employees II easy or hard?
Project Employees II is classified as an Easy database problem. It mainly tests familiarity with SQL GROUP BY, COUNT aggregation, and filtering grouped results using HAVING or a MAX subquery.
Project Employees II Python/Java solution
This problem is a database query question, so the primary solution is written in SQL (MySQL on LeetCode). Python or Java are typically not required unless the query is executed through an ORM or database connector.
How to solve Project Employees II in O(n)?
Use SQL aggregation. Group rows by project_id and compute COUNT(employee_id). Then filter using HAVING COUNT(employee_id) = (SELECT MAX(cnt) FROM (SELECT COUNT(*) cnt FROM Project GROUP BY project_id) t). The database processes the table in linear time relative to the number of rows.
What is the best approach for Project Employees II?
The best approach groups rows by project_id and counts employees using COUNT(employee_id). A subquery calculates the maximum employee count across all projects, and the outer query returns projects whose counts match that value. This handles ties correctly and runs in O(n) time.
Is Project Employees II asked at Google/Amazon/Meta?
SQL aggregation and GROUP BY problems frequently appear in interviews at companies like Amazon, Meta, and Google. Variants that require finding maximum counts, top groups, or ranking results are especially common in data-focused roles.
What data structure is used in Project Employees II?
Relational database aggregation is the main concept. Internally, the database engine builds grouped results similar to a hash map keyed by project_id, where each key stores the running COUNT of employees.
What is the time complexity of Project Employees II?
The SQL query scans the Project table and aggregates rows using GROUP BY. This requires O(n) time where n is the number of records. The grouped result set stores one entry per project, giving O(k) space where k is the number of unique projects.

Ready to solve this problem?

Practice Project Employees II with our built-in code editor and test cases.

Practice on FleetCode