Loading...
Loading...
You are working with a company's HR database. The Employee table stores information about all employees, including their salary and the ID of their manager. Your task is to identify which employees earn a higher salary than their direct manager.
Employee
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| salary | int |
| managerId | int |
+-------------+---------+
id is the primary key.managerId is a foreign key referencing id. If an employee has no manager, managerId is NULL.Write a SQL query that returns the names of all employees who earn strictly more than their direct manager. The result table can be returned in any order.
Input: The Employee table (provided as a CSV-like representation for testing).
Output: A single-column result named Employee containing the names of qualifying employees.
Input Table:
id | name | salary | managerId
1 | Joe | 70000 | 3
2 | Henry | 80000 | 4
3 | Sam | 60000 | NULL
4 | Max | 90000 | NULL
Output:
Employee
--------
Joe
Explanation: Joe (salary: 70,000) reports to Sam (salary: 60,000). Since 70,000 > 60,000, Joe qualifies. Henry (salary: 80,000) reports to Max (salary: 90,000) — Henry does not qualify.
Input Table:
id | name | salary | managerId
1 | Alice | 120000 | NULL
2 | Bob | 130000 | 1
3 | Charlie | 115000 | 1
Output:
Employee
--------
Bob
Explanation: Both Bob and Charlie report to Alice (120,000). Bob (130,000) earns more, Charlie (115,000) does not.
Employee table with itself — one alias for the employee, another for the manager — linking on employee.managerId = manager.id.WHERE clause comparing employee.salary > manager.salary.NULL managerId are top-level executives with no manager; they should be automatically excluded by an INNER JOIN since there is no matching manager row.1 <= number of rows in Employee <= 10^4 1 <= id <= 10^4 1 <= salary <= 10^6 managerId is either NULL or a valid id in the Employee table All employee names are non-empty strings of length <= 255 No two employees share the same id
Employee.Preferred Approach:
SELECT e.name AS Employee
FROM Employee e
JOIN Employee m ON e.managerId = m.id
WHERE e.salary > m.salary;