#1978

Employees Whose Manager Left the Company

Easy
DatabaseJOIN operationsSubqueriesFiltering with conditions
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

This approach uses a single query to find employees with a salary less than $30,000 and checks for their managers in a more efficient way by using a LEFT JOIN.

⚙️

Algorithm

3 steps
  1. 1Step 1: Perform a LEFT JOIN on the Employees table to find employees with a salary less than $30,000 and their corresponding manager.
  2. 2Step 2: Filter the results to include only those employees whose manager_id is not found in the Employees table (i.e., the manager has left).
  3. 3Step 3: Select the employee_id of these filtered employees.
solution.py1 lines
1SELECT e.employee_id FROM Employees e LEFT JOIN Employees m ON e.manager_id = m.employee_id WHERE e.salary < 30000 AND m.employee_id IS NULL;

Complexity note: The complexity is O(n) because we are scanning through the Employees table once and performing a join operation, which is efficient.

  • 1Understanding how to check for non-existent references (managers) is crucial in relational databases.
  • 2Using JOINs can significantly reduce the number of queries and improve performance.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.