Find employees who are working on more than two projects.
💡 Model Answer
To find employees assigned to more than two projects, you can use a GROUP BY with a HAVING clause. Assuming you have an employees table (employees) and a junction table (employee_projects) that links employees to projects, the query would look like this:
SELECT e.employee_id,
e.first_name,
e.last_nameFROM employees e
JOIN employee_projects ep ON e.employee_id = ep.employee_id
GROUP BY e.employee_id, e.first_name, e.last_name
HAVING COUNT(DISTINCT ep.project_id) > 2;
Explanation:
- The JOIN brings together each employee with their project assignments.
- GROUP BY aggregates rows per employee.
- COUNT(DISTINCT ep.project_id) counts unique projects for each employee.
- HAVING filters groups where the count exceeds 2.
Complexity: The query scans the employee_projects table once (O(n)) and performs a hash/grouping operation. The overall time is linear in the number of rows in employee_projects, and the memory usage is proportional to the number of distinct employees. This is efficient for typical relational databases and scales well with large datasets.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500