EasyGROUP BY & Aggregation
Count employees by department
The query
SQL
SELECT
d.dept_name,
COUNT(e.emp_id) AS employee_count
FROM departments d
LEFT JOIN employees e ON d.dept_id = e.dept_id
GROUP BY d.dept_id, d.dept_name
ORDER BY employee_count DESC;Note
“Include non-aggregated columns in GROUP BY. Using LEFT JOIN to include empty departments.