HomeInterview QuestionsWhat is a better approach to find employees workin…

What is a better approach to find employees working on projects greater than or equal to the average number of projects per employee?

🟡 Medium Conceptual Mid level
1Times asked
Jul 2026Last seen
Jul 2026First seen

💡 Model Answer

A more efficient way is to use a window function to compute the average number of projects per employee in a single scan, then filter the employees whose count meets or exceeds that average. For example: SELECT e.first_name, e.last_name, project_cnt FROM ( SELECT e.id, e.first_name, e.last_name, COUNT(ep.project_id) AS project_cnt, AVG(COUNT(ep.project_id)) OVER () AS avg_projects FROM employees e JOIN employee_projects ep ON e.id = ep.employee_id GROUP BY e.id, e.first_name, e.last_name ) sub WHERE project_cnt >= avg_projects; This query performs only one aggregation pass, avoids a nested subquery, and is easier to read. Complexity is O(n) for the join and O(m) for the aggregation, where n is the number of employee‑project rows and m is the number of employees. Using a window function also allows you to add additional metrics (e.g., percentiles) without extra subqueries.

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