Me: Using GROUP BY and COUNT():
SELECT d.dept_name,
COUNT(e.id) AS employee_count
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id
GROUP BY d.dept_name;
✔ Why it works:
– COUNT() tallies employees per department by counting non-null IDs.
– GROUP BY segments the results by department name for aggregated output.
– INNER JOIN links employees to departments only where IDs match, avoiding nulls—add ORDER BY employee_count DESC to sort by largest teams first!
💬 Tap ❤️ if you're learning something new!