The Employee Bonus coding problem is a fundamental database query challenge. You are given two tables: an Employee table containing names and IDs, and a Bonus table containing IDs and bonus amounts. The task is to report the name and bonus amount of each employee with a bonus less than 1000. Crucially, if an employee does not have a bonus at all (i.e., they are missing from the Bonus table), they should still be included in the results, as their effective bonus is less than 1000.
This is a classic Database interview question used by companies like Uber and Microsoft to test a candidate's understanding of SQL Joins, specifically the difference between an INNER JOIN and a LEFT JOIN. It also checks for the ability to handle NULL values correctly. In real-world software engineering, data is often fragmented across tables, and knowing how to retrieve "missing" or "optional" relationships is a daily requirement for backend and data engineers.
The core pattern here is the Left Outer Join. By performing a LEFT JOIN from the Employee table to the Bonus table on the employee ID, you ensure that every employee is represented in the resulting set. If no matching bonus exists, the bonus column will contain NULL. You then use a WHERE clause to filter for cases where the bonus is either less than 1000 or is NULL.
Imagine these two tables: Employees:
Bonus:
When we join them:
bonus < 1000).NULL (Matches the criteria because no bonus is technically less than 1000).
The output would show Alice and Charlie.WHERE bonus < 1000 often fails in SQL because NULL < 1000 evaluates to UNKNOWN, not TRUE. You must explicitly check OR bonus IS NULL.Bonus to Employee using a left join would only show employees who have a bonus record, which is the opposite of what's needed.Always clarify the requirements regarding missing data. Whenever a problem mentions "employees who did/did not receive X," your mind should immediately go to LEFT JOIN and IS NULL. Practice writing these queries and remember that in many SQL dialects, the order of tables in a LEFT JOIN matters significantly.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Project Employees I | Easy | Solve | |
| Triangle Judgement | Easy | Solve | |
| Duplicate Emails | Easy | Solve | |
| Recyclable and Low Fat Products | Easy | Solve | |
| Fix Names in a Table | Easy | Solve |