Skip to main content
Beginnersql query Verified

Write a SQL query that returns the number of employees in each department, including departments with no employees.

ReportedSource: GeeksforGeeks interview experience: Amazon Data Engineer

Quick answer

departments LEFT JOIN employees, GROUP BY department, COUNT(e.employee_id) (not COUNT(*)) so empty departments show 0.

Constraints

  • Tables: departments(department_id, department_name) and employees(employee_id, name, department_id). employees.department_id can be NULL.
  • Output columns in order: department_id, department_name, employee_count.
  • Sort by employee_count descending, then department_id ascending.
  • The reference runs on SQLite; the same query works on PostgreSQL and MySQL.

Sample I/O

in: CREATE TABLE departments (department_id INTEGER PRIMARY KEY, department_name TEXT); CREATE TABLE employees (employee_id INTEGER PRIMARY KEY, name TEXT, department_id INTEGER); INSERT INTO departments VALUES (1, 'Engineering'), (2, 'HR'), (3, 'Sales'); INSERT INTO employees VALUES (1, 'Asha', 1), (2, 'Ben', 1), (3, 'Chen', 1), (4, 'Dev', 3);

out: 1 Engineering 3 3 Sales 1 2 HR 0

in: CREATE TABLE departments (department_id INTEGER PRIMARY KEY, department_name TEXT); CREATE TABLE employees (employee_id INTEGER PRIMARY KEY, name TEXT, department_id INTEGER); INSERT INTO departments VALUES (1, 'Data'), (2, 'Ops'); INSERT INTO employees VALUES (1, 'A', 1), (2, 'B', 2), (3, 'C', 2);

out: 2 Ops 2 1 Data 1

in: CREATE TABLE departments (department_id INTEGER PRIMARY KEY, department_name TEXT); CREATE TABLE employees (employee_id INTEGER PRIMARY KEY, name TEXT, department_id INTEGER); INSERT INTO departments VALUES (1, 'Data'), (2, 'Ops');

out: 1 Data 0 2 Ops 0

sql
Loading editor…
Result

Sign up to run your code in the sandbox.

Sign up to run code and get an AI review

You can read and edit the challenge freely — running it in the sandbox and getting AI feedback need a free account.

Sign up free

Hints

Reference solution

Query. Start from departments and LEFT JOIN employees, so empty departments survive the join, then count a column from the employee side.

query.sql
SELECT d.department_id,
       d.department_name,
       COUNT(e.employee_id) AS employee_count
FROM departments d
LEFT JOIN employees e
       ON e.department_id = d.department_id
GROUP BY d.department_id, d.department_name
ORDER BY employee_count DESC;

Details interviewers check.

  • COUNT(e.employee_id) counts only non-null values, so an empty department shows 0. COUNT(*) would wrongly show 1 because the LEFT JOIN produces one row of nulls.
  • Group by the department key, not only the name (two departments can share a name).
  • If only the employees table exists: SELECT department_id, COUNT(*) FROM employees GROUP BY department_id (departments without employees cannot appear).

Follow-ups. Departments with more than 10 people: add HAVING COUNT(e.employee_id) > 10. Percentage of the company: COUNT(...) * 100.0 / SUM(COUNT(...)) OVER ().

See the concept: Join fan-out

Join fan-out
order_lines: one row per line
O101line 1₹300
payments: one row per attempt
O101P101FAILED
O101P102SUCCESS
lines JOIN payments ON order_id
line 1P101₹300
line 1P102₹300
SUM(line_revenue)₹600

Wrong: the single ₹300 line is counted once per payment attempt.

Fix: aggregate payments to one row per order first

line 1, paid, ₹300, so SUM = ₹300

One order line, two payment attempts: a direct join doubles the revenue. Aggregating payments to order grain first keeps one row per line.

Discussion