Skip to content
Quantoros

Return the names of managers (anyone with at least one report) who earn strictly more than EVERY one of their direct…

SQLLevel: IntermediateJoins and self-joins

Return the names of managers (anyone with at least one report) who earn strictly more than EVERY one of their direct reports.

Table definitions (from the source exercise):

CREATE TABLE employees(emp_id INTEGER PRIMARY KEY, name TEXT, department_id INTEGER, manager_id INTEGER, salary INTEGER, hire_date TEXT, job_title TEXT, gender TEXT);
Hints
  • Define one output row and the source-table keys.
  • Check duplicates, NULLs and the order of filtering and aggregation.

Answer

Approach: Join manager to reports, group by manager, HAVING manager salary > MAX(report salary).

SELECT m.name
FROM employees m
JOIN employees e ON e.manager_id = m.emp_id
GROUP BY m.emp_id, m.name, m.salary
HAVING m.salary > MAX(e.salary);

The source uses SQLite with a MySQL compatibility layer. Date and string functions are dialect dependent; the query is preserved as published.

Source and licence: SQL Practice Lab — Ebrahim Sharifi · MIT

Related questions