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
๐Ÿ“ Employee FilesAlice (ID: 1)Bob (ID: 2)Charlie (ID: 3)Diana (ID: 4)๐Ÿ’ฐ Bonus RecordsID: 1 โ†’ $800ID: 3 โ†’ $1500ID: 4 โ†’ $200LEFT JOIN๐Ÿ“Š Merged ResultsAlice | $800 | โœ“ Include (< 1000)Bob | NULL | โœ“ Include (no bonus)Charlie | $1500 | โœ— Exclude (โ‰ฅ 1000)Diana | $200 | โœ“ Include (< 1000)Final Answer:Alice, Bob, Diana๐Ÿ’ก LEFT JOIN preserves all employees, NULL indicates no bonus
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

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space for result set only, database handles JOIN efficiently

n
2n
โœ“ 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
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
42.8K Views
High Frequency
~8 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen