Employees earning more than their manager
Return employee ID, employee name, and manager name when the employee salary strictly exceeds their manager salary. Exclude missing managers and NULL salary comparisons. Return only the requested columns and follow the specified ordering. This question probes a self join pairs each employee with exactly their manager.
Input
Setup script defining employees(id INT, name VARCHAR(30), department_id INT, manager_id INT, salary INT).
Output
id, employee_name, manager_name; ascending employee ID.
Constraints
0–10000 rows per table. IDs are unique and non-NULL unless stated otherwise; foreign keys may be NULL. Numeric amounts are integers from 0 to 1000000. Dates are valid YYYY-MM-DD text. Text uses case-sensitive comparison; test data has consistent case. Duplicate business values and NULLs are allowed unless excluded by the task.
Example
Tables and sample rows
CREATE TABLE employees(id INT, name VARCHAR(30), department_id INT, manager_id INT, salary INT); INSERT INTO employees VALUES (1,'Ana',10,NULL,100),(2,'Ben',10,1,200),(3,'Cara',20,1,200),(4,'Dan',20,3,300),(5,'Eve',NULL,NULL,NULL);
Expected result
id | employee_name | manager_name 2 | Ben | Ana 3 | Cara | Ana 4 | Dan | Cara
Another dataset (1)
CREATE TABLE employees(id INT, name VARCHAR(30), department_id INT, manager_id INT, salary INT); INSERT INTO employees VALUES (1,'Boss',10,NULL,50),(2,'Worker',10,1,100),(3,'Null',10,1,NULL);
Expected result
id | employee_name | manager_name 2 | Worker | Boss
Covers missing matches, ties, or NULL values without relying on insertion order.
Hints (2)
- Join the table to itself using different aliases.
- Compare salaries only after matching the manager.
Target complexity: time O(n) · space O(n)
