Back to blog
Feb 05, 2025
10 min read

Employee Bonus

Select all employees with a bonus less than 1000, returning their name and bonus amount.

Difficulty: Easy | Acceptance: 77.40% | Paid: No Topics: Database

Table: Employee +-------------+---------+ | Column Name | Type | +-------------+---------+ | empId | int | | name | varchar | | supervisor | int | | salary | int | +-------------+---------+ empId is the primary key column for this table. Each row of this table indicates the name of an employee, their id, their salary, and the id of their manager.

Table: Bonus +-------------+------+ | Column Name | Type | +-------------+------+ | empId | int | | bonus | int | +-------------+------+ empId is the primary key column for this table. empId is a foreign key to empId from the Employee table. Each row of this table contains the id of an employee and the bonus they have been given.

Write an SQL query to report the name and bonus amount of each employee with a bonus less than 1000.

Return the result table in any order.

Examples

Example 1:

Input: Employee table: +-------+--------+------------+--------+ | empId | name | supervisor | salary | +-------+--------+------------+--------+ | 3 | Brad | null | 4000 | | 1 | John | 3 | 1000 | | 2 | Dan | 3 | 2000 | | 4 | Thomas | 3 | 4000 | +-------+--------+------------+--------+ Bonus table: +-------+-------+ | empId | bonus | +-------+-------+ | 2 | 500 | | 4 | 2000 | +-------+-------+ Output: +-------+-------+ | name | bonus | +-------+-------+ | Brad | null | | John | null | | Dan | 500 | +-------+-------+ Explanation:

  • Brad and John do not have a bonus record, so their bonus is null.
  • Dan has a bonus of 500, which is less than 1000.
  • Thomas has a bonus of 2000, which is not less than 1000, so he is not included in the result.

Constraints

1 <= Employee.empId <= 100
1 <= Employee.salary <= 100000
0 <= Bonus.bonus <= 100000

Left Join Approach

Intuition Use LEFT JOIN to include all employees, then filter for bonuses less than 1000 or null.

Steps

  • Perform LEFT JOIN between Employee and Bonus tables on empId
  • Filter rows where bonus < 1000 OR bonus IS NULL
  • Select name and bonus columns
python
import sqlite3

def get_employee_bonus():
    conn = sqlite3.connect(":memory:")
    cursor = conn.cursor()
    
    query = """
    SELECT name, bonus
    FROM Employee
    LEFT JOIN Bonus ON Employee.empId = Bonus.empId
    WHERE bonus &lt; 1000 OR bonus IS NULL
    """
    
    cursor.execute(query)
    return cursor.fetchall()

# LeetCode SQL solution:
# SELECT name, bonus
# FROM Employee
# LEFT JOIN Bonus ON Employee.empId = Bonus.empId
# WHERE bonus &lt; 1000 OR bonus IS NULL

Complexity

  • Time: O(n + m) where n is the number of employees and m is the number of bonus records
  • Space: O(k) where k is the number of result rows
  • Notes: LEFT JOIN ensures employees without bonus records are included

Left Join with COALESCE Approach

Intuition Use LEFT JOIN with COALESCE to handle null values by converting them to 0, then filter.

Steps

  • Perform LEFT JOIN between Employee and Bonus tables on empId
  • Use COALESCE to convert null bonus values to 0
  • Filter where COALESCE(bonus, 0) < 1000
  • Select name and bonus columns
python
import sqlite3

def get_employee_bonus():
    conn = sqlite3.connect(":memory:")
    cursor = conn.cursor()
    
    query = """
    SELECT name, bonus
    FROM Employee
    LEFT JOIN Bonus ON Employee.empId = Bonus.empId
    WHERE COALESCE(bonus, 0) &lt; 1000
    """
    
    cursor.execute(query)
    return cursor.fetchall()

# LeetCode SQL solution:
# SELECT name, bonus
# FROM Employee
# LEFT JOIN Bonus ON Employee.empId = Bonus.empId
# WHERE COALESCE(bonus, 0) &lt; 1000

Complexity

  • Time: O(n + m) where n is the number of employees and m is the number of bonus records
  • Space: O(k) where k is the number of result rows
  • Notes: COALESCE simplifies the WHERE clause by treating null as 0

Subquery Approach

Intuition Use a subquery to find employees with bonus < 1000, then combine with employees without any bonus.

Steps

  • Select employees with bonus < 1000 using a subquery
  • Use UNION to combine with employees who have no bonus record
  • Select name and bonus columns
python
import sqlite3

def get_employee_bonus():
    conn = sqlite3.connect(":memory:")
    cursor = conn.cursor()
    
    query = """
    SELECT name, bonus
    FROM Employee
    LEFT JOIN Bonus ON Employee.empId = Bonus.empId
    WHERE empId NOT IN (SELECT empId FROM Bonus WHERE bonus &gt;= 1000)
    """
    
    cursor.execute(query)
    return cursor.fetchall()

# LeetCode SQL solution:
# SELECT name, bonus
# FROM Employee
# LEFT JOIN Bonus ON Employee.empId = Bonus.empId
# WHERE empId NOT IN (SELECT empId FROM Bonus WHERE bonus &gt;= 1000)

Complexity

  • Time: O(n + m) where n is the number of employees and m is the number of bonus records
  • Space: O(k) where k is the number of result rows
  • Notes: Subquery approach can be less efficient on large datasets due to NOT IN operation