Write an SQL query to count the number of employees in each department.
💡 Model Answer
You can use GROUP BY to aggregate employees per department and COUNT to get the number of rows in each group. The query is:
SELECT department, COUNT(*) AS employee_count
FROM Employee
GROUP BY department
ORDER BY department;
This query groups all rows by the department column, counts how many rows belong to each group, and returns the department name along with the count. The ORDER BY clause is optional but makes the output easier to read. The time complexity is O(n) because each row is processed once during grouping.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500