← Back to SQL, page 15

sql-0287 · Easy fresher

Write a query that lists managers with more than two direct reports.

Group employees by manager_id, keep groups with COUNT(*) > 2, and join to employees again for the manager's name. Exclude NULL manager_id so the top of the hierarchy is not treated as a manager group.

Full answer
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT m.name AS manager, COUNT(*) AS direct_reports
FROM employees e JOIN employees m ON m.id = e.manager_id
GROUP BY m.id, m.name
HAVING COUNT(*) > 2
ORDER BY direct_reports DESC;
managerdirect_reports
Asha4

Asha manages Ravi, Mei, Tom and Diego, so she has four direct reports. Tom manages Sara and Lena (two), and Diego manages Omar (one), so neither passes the filter.

The join direction matters: e is the report and m the manager, joined on m.id = e.manager_id. Grouping by m.id rather than by name keeps two managers with the same name apart. Counting all levels below a manager, not just direct reports, needs the recursive query from the CTE questions.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT m.name AS manager, COUNT(*) AS direct_reports
FROM employees e JOIN employees m ON m.id = e.manager_id
GROUP BY m.id, m.name
HAVING COUNT(*) > 2
ORDER BY direct_reports DESC;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Group by manager_id to count direct reports
  • HAVING filters the counts
  • Join back for the manager's name
  • Indirect reports need recursion

Common mistakes

  • Counting all descendants when the question asks for direct reports
  • Treating NULL manager_id as a manager

Follow-ups

  • How would you count all reports at every level?
  • How would you list managers with no reports?

Tips

  • Clarify direct versus indirect