Employee Bonus - Problem
You're managing a company's HR system with two important tables: Employee and Bonus. Your task is to find all employees who either received a small bonus (less than $1000) or no bonus at all.
This is a classic SQL problem that tests your understanding of LEFT JOIN operations and handling NULL values - essential skills for any database developer!
Given:
- Employee Table: Contains employee details (ID, name, supervisor, salary)
- Bonus Table: Contains bonus information (employee ID and bonus amount)
Goal: Return the name and bonus of employees who have bonus < 1000 OR no bonus entry.
Note: Some employees might not have any entry in the Bonus table, which should be treated as having no bonus.
Input & Output
example_1.sql โ Basic Case
$
Input:
Employee: [(1,'Alice',null,70000), (2,'Bob',1,80000), (3,'Charlie',2,90000)]
Bonus: [(1,800), (3,1500)]
โบ
Output:
[('Alice',800), ('Bob',null)]
๐ก Note:
Alice has bonus 800 < 1000 โ, Bob has no bonus entry โ, Charlie has bonus 1500 โฅ 1000 โ
example_2.sql โ All Have Bonuses
$
Input:
Employee: [(1,'John',null,60000), (2,'Jane',1,75000)]
Bonus: [(1,500), (2,1200)]
โบ
Output:
[('John',500)]
๐ก Note:
John has bonus 500 < 1000 โ, Jane has bonus 1200 โฅ 1000 โ
example_3.sql โ No Bonuses
$
Input:
Employee: [(1,'Mary',null,55000), (2,'Tom',1,65000)]
Bonus: []
โบ
Output:
[('Mary',null), ('Tom',null)]
๐ก Note:
Both employees have no bonus entries, so both should be included with null bonuses
Visualization
Tap to expand
Understanding the Visualization
1
Setup Tables
We have Employee records (everyone) and Bonus records (selective)
2
LEFT JOIN Magic
Merge tables keeping ALL employees, bonus becomes NULL if missing
3
Apply Business Logic
Filter for small bonuses (< 1000) OR no bonuses (NULL)
4
Present Results
Return employee names with their bonus status
Key Takeaway
๐ฏ Key Insight: LEFT JOIN is perfect for "optional" relationships - it keeps all records from the left table while gracefully handling missing matches as NULL values.
Time & Space Complexity
Time Complexity
O(n + m)
Single pass through both tables with efficient JOIN operation
โ Linear Growth
Space Complexity
O(k)
Space for result set only, database handles JOIN efficiently
โ Linear Space
Constraints
- 1 โค Employee.empId โค 104
- 1 โค Employee.name.length โค 20
- 0 โค Bonus.bonus โค 106
- Employee.empId is unique
- Some employees may not have bonus entries
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code