Interview Question

Count employees by department

Group employee rows by department and count each group.

💡 Concept ✅ Quick Revision 🗃️ SQL

Answer

Employee counts per department use COUNT and GROUP BY. • COUNT(*) counts employee rows in each group. • An inner query omits departments with no employees. • Use a LEFT JOIN from departments to include zero-count departments.

💡 SQL Example

SELECT department_id, COUNT(*) AS employee_count FROM employees GROUP BY department_id;

Result

One employee count per represented department

⚡ Quick Revision

Group employee rows by department and count each group.