Calculate Special Bonus - Problem

๐ŸŽฏ Employee Bonus Calculator

You're tasked with implementing a company's special bonus system! Given an employee database, you need to calculate each employee's bonus based on two specific criteria:

  • Employee ID is odd AND
  • Employee name does NOT start with 'M'

If both conditions are met, the employee receives a 100% salary bonus (bonus = salary). Otherwise, they receive no bonus (bonus = 0).

Input: A table Employees with columns employee_id (int), name (varchar), and salary (int).

Output: Return employee_id and bonus for each employee, ordered by employee_id.

Example: Employee with ID 3, name "Alice", salary 1000 โ†’ gets bonus 1000 (odd ID, name doesn't start with M). Employee with ID 2, name "Bob", salary 2000 โ†’ gets bonus 0 (even ID).

Input & Output

example_1.sql โ€” Basic Example
$ Input: Employees table: +-------------+-------+--------+ | employee_id | name | salary | +-------------+-------+--------+ | 2 | Meir | 3000 | | 3 | Michael| 3800 | | 7 | Addilyn| 7400 | | 8 | Juan | 6100 | | 9 | Kannon | 7700 | +-------------+-------+--------+
โ€บ Output: +-------------+-------+ | employee_id | bonus | +-------------+-------+ | 2 | 0 | | 3 | 0 | | 7 | 7400 | | 8 | 0 | | 9 | 7700 | +-------------+-------+
๐Ÿ’ก Note: Employee 2: Even ID โ†’ bonus = 0. Employee 3: Odd ID but name starts with 'M' โ†’ bonus = 0. Employee 7: Odd ID and name doesn't start with 'M' โ†’ bonus = salary = 7400. Employee 8: Even ID โ†’ bonus = 0. Employee 9: Odd ID and name doesn't start with 'M' โ†’ bonus = salary = 7700.
example_2.sql โ€” Edge Cases with Names
$ Input: Employees table: +-------------+-------+--------+ | employee_id | name | salary | +-------------+-------+--------+ | 1 | Mary | 2000 | | 5 | mike | 1500 | | 11 | Alice | 3000 | +-------------+-------+--------+
โ€บ Output: +-------------+-------+ | employee_id | bonus | +-------------+-------+ | 1 | 0 | | 5 | 1500 | | 11 | 3000 | +-------------+-------+
๐Ÿ’ก Note: Employee 1: Odd ID but name starts with 'M' (uppercase) โ†’ bonus = 0. Employee 5: Odd ID and name starts with 'm' (lowercase, not 'M') โ†’ bonus = 1500. Employee 11: Odd ID and name doesn't start with 'M' โ†’ bonus = 3000. Note: Case sensitivity matters - 'M' โ‰  'm'.
example_3.sql โ€” Single Employee
$ Input: Employees table: +-------------+-------+--------+ | employee_id | name | salary | +-------------+-------+--------+ | 4 | Bob | 5000 | +-------------+-------+--------+
โ€บ Output: +-------------+-------+ | employee_id | bonus | +-------------+-------+ | 4 | 0 | +-------------+-------+
๐Ÿ’ก Note: Employee 4: Even ID (4 % 2 = 0) โ†’ bonus = 0, regardless of the name not starting with 'M'.

Constraints

  • 1 โ‰ค employee_id โ‰ค 1000
  • 1 โ‰ค name.length โ‰ค 20
  • 1000 โ‰ค salary โ‰ค 106
  • Name consists of uppercase and lowercase English letters
  • Employee_id is unique (primary key)

Visualization

Tap to expand
Employee Bonus Decision TreeEmployeeEntersIs ID Odd?(id % 2 = 1)Bonus = 0(Even ID)Name starts with 'M'?(Check first letter)Bonus = 0(Name starts with M)Bonus = Salary(Both conditions met)NOYESYESNO
Understanding the Visualization
1
Gate 1: ID Check
Employee approaches first gate - is their ID number odd?
2
Gate 2: Name Check
If they pass Gate 1, check if their name starts with 'M'
3
Prize Distribution
Only employees who pass both gates receive the full salary bonus
Key Takeaway
๐ŸŽฏ Key Insight: Use conditional SQL logic (CASE/IF) to implement business rules efficiently in a single query pass
Asked in
Amazon 25 Microsoft 18 Google 15 Meta 12
42.0K Views
High Frequency
~8 min Avg. Time
1.9K 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