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
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
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 freeHints
Reference solution
Query. Start from departments and LEFT JOIN employees, so empty departments survive the join, then count a column from the employee side.
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
| O101 | line 1 | ₹300 |
| O101 | P101 | FAILED |
| O101 | P102 | SUCCESS |
| line 1 | P101 | ₹300 |
| line 1 | P102 | ₹300 |
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