๐๐ป๐๐ฒ๐ฟ๐๐ถ๐ฒ๐๐ฒ๐ฟ: How would you count how many employees are in each department?
๐ ๐ฒ: Iโd use the
GROUP BY clause with COUNT(*) to aggregate employee counts per department.๐น Query:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
โ Why it works:
โ
GROUP BY groups rows by department โ
COUNT(*) counts employees in each group โ Clean, scalable, and works with large datasets
๐ Bonus Insight:
To filter only departments with more than 5 employees:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
โ
HAVING filters aggregated results โ Useful in dashboards, reports, and business logic
๐ฌ Tap โค๏ธ for more SQL interview tips!