SQLQuestion 02 of 14
GROUP BY and HAVING — managers with at least 2 people
Answer out loud or in your head first, then compare.
Answer
I group employees by their manager and filter the already-counted groups.
SQL
-- employees(id, name, manager_id)
SELECT manager_id, COUNT(*) AS team_size
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING COUNT(*) >= 2;To be precise: WHERE removes rows before aggregation, HAVING filters its results. These are direct reports, not everyone below in the hierarchy.
Pitfall: a later JOIN to a 1:N table can multiply the employees and inflate the count.
How did it go? Be honest — “Not yet” puts the question on your review list.