Sponsored
This approach uses a self join on the Employee table to compare each employee's salary with their manager's salary. The core idea is to join the Employee table with itself where the manager ID of an employee in one instance of the table matches the ID of the manager in the other instance. We then simply select those employees where the salary is greater than that of the joined manager's.
Time Complexity: O(n^2), where n is the number of employees (due to the join operation).
Space Complexity: O(n), as it requires storing the join results.
1SELECT e1.name AS Employee FROM Employee e1 JOIN Employee e2 ON e1.managerId = e2.id WHERE e1.salary > e2.salary
The SQL solution uses a self join query where the Employee table is joined with itself. e1.managerId = e2.id
matches each employee with its manager, and the condition e1.salary > e2.salary
filters out only those employees whose salary is greater than their manager's salary.
This approach involves using a HashMap to store the salaries of managers. First, iterate over the table to fill the HashMap with manager ID as the key and the corresponding manager's salary as the value. Then, iterate over the employees again to check if their salary is greater than their manager's salary using the hashmap for comparison.
Time Complexity: O(n), where n is the number of employees (as each employee is processed twice).
Space Complexity: O(n), due to the additional storage used by the hashmap.
1def find_high_earning_employees(employee_table):
2 manager_salaries
In the Python implementation, a dictionary manager_salaries
is used to store the salaries of all employees, which allows quick lookup of any manager's salary. For each employee, if their salary is greater than their manager's salary (found via manager_salaries[manager_id]
), they are added to the list of high_earners.