InterviewPrepKit

Home / SQL / Aggregation & Grouping

Count Employees Per Department

easy
Solving tips
  • Reach for GROUP BY the moment you see "per" or "for each" in the prompt.
  • COUNT(*) counts every row in the group, but COUNT(col) skips rows where col is NULL — pick deliberately.
  • Every non-aggregated column in SELECT must appear in GROUP BY.

Given an employee roster, report how many people work in each department and how many of them report to a manager.

Schema

CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  department TEXT NOT NULL,
  manager_id INTEGER   -- NULL for employees with no manager
);

Sample data:

idnamedepartmentmanager_id
1AliceEngineeringNULL
2BobEngineering1
3CarolEngineering1
4DaveSalesNULL
5EveSales4
6FrankMarketingNULL

Task

For each department, return:

  • department
  • total_employees — the number of employees in the department
  • employees_with_manager — the number of employees in the department that have a non-NULL manager_id

Order the result by department ascending.

Expected output

departmenttotal_employeesemployees_with_manager
Engineering32
Marketing10
Sales21
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.