Domain 1EasyCORE

Employee Bonus

**Restate the problem in plain language:** We are given two tables, `Employee` and `Bonus`. The `Employee` table contains employee details such as `employee_id`, `name`, and `salary`. The `Bonus` table contains bonus information for employees, including `employee_id` and `bonus`. We need to find the names of all employees who do not have a bonus.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 0

577. Employee Bonus

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 0
Acceptance rate: 72.0%


Problem Breakdown

Restate the problem in plain language: We are given two tables, Employee and Bonus. The Employee table contains employee details such as employee_id, name, and salary. The Bonus table contains bonus information for employees, including employee_id and bonus. We need to find the names of all employees who do not have a bonus.

Inputs:

  • Two tables:
    • Employee: with columns employee_id, name, and salary.
    • Bonus: with columns employee_id and bonus.

Outputs:

  • A list of employee names from the Employee table where there is no corresponding entry in the Bonus table.

Implicit constraints or tricky edge cases:

  • The Bonus table might be empty.
  • There could be duplicate entries in either table, but we assume each employee_id is unique within its respective table.
  • The employee_id in both tables should match for a valid bonus entry.

Intuition

Brute-force approach:

  1. Iterate through each employee in the Employee table.
  2. For each employee, check if there is an entry in the Bonus table with the same employee_id.
  3. If no matching entry is found, add the employee’s name to the result list.

This approach would have a time complexity of O(n * m), where n is the number of employees and m is the number of bonus entries. This is inefficient for large datasets.

Key insight: Instead of checking each employee against all bonuses, we can use SQL joins to efficiently find employees without bonuses. Specifically, we can use a LEFT JOIN between the Employee and Bonus tables on employee_id. Employees without bonuses will have NULL values in the bonus column of the result.

Approach

  1. Use a LEFT JOIN:

    • Join the Employee table with the Bonus table on employee_id.
    • This will include all employees and their corresponding bonuses, if any.
  2. Filter out employees without bonuses:

    • Use a WHERE clause to filter rows where the bonus is NULL.
  3. Select the required columns:

    • Select only the name column from the Employee table for the final output.

Complexity Analysis

  • Time complexity: O(n + m), where n is the number of employees and m is the number of bonuses. The join operation processes each row in both tables once.
  • Space complexity: O(k), where k is the number of employees without bonuses. This is the space required to store the result set.

Solution

# SQL query to find employees without bonuses
SELECT e.name 
FROM Employee e 
LEFT JOIN Bonus b ON e.employee_id = b.employee_id 
WHERE b.bonus IS NULL;

Edge Cases Handled

  • Empty Bonus table: If there are no bonus entries, the join will result in all employees having NULL in the bonus column, and they will all be selected.
  • No matching employee_id: Employees without any bonuses will naturally have NULL values in the bonus column after the join, ensuring they are included in the final result.