{x}
blog image

Calculate Special Bonus

Table: Employees

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| employee_id | int     |
| name        | varchar |
| salary      | int     |
+-------------+---------+
employee_id is the primary key (column with unique values) for this table.
Each row of this table indicates the employee ID, employee name, and salary.

 

Write a solution to calculate the bonus of each employee. The bonus of an employee is 100% of their salary if the ID of the employee is an odd number and the employee's name does not start with the character 'M'. The bonus of an employee is 0 otherwise.

Return the result table ordered by employee_id.

The result format is in the following example.

 

Example 1:

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  |
+-------------+-------+
Explanation: 
The employees with IDs 2 and 8 get 0 bonus because they have an even employee_id.
The employee with ID 3 gets 0 bonus because their name starts with 'M'.
The rest of the employees get a 100% bonus.

Solution Explanation and Code

This problem requires calculating a special bonus for employees based on their ID and name. The bonus is equal to their salary if their ID is odd and their name doesn't start with 'M'; otherwise, it's 0. The solution involves querying the Employees table and applying conditional logic to calculate the bonus for each employee.

Approach

The most efficient approach is to use a conditional statement within the SQL query itself. This avoids the need for any pre-processing or post-processing of the data. We use IF (or a similar conditional function depending on the specific SQL dialect) to check the conditions and assign the bonus accordingly. Then, we order the results by employee_id.

Time and Space Complexity

  • Time Complexity: O(N log N), where N is the number of employees in the Employees table. This is dominated by the sorting operation (ORDER BY). The conditional check within the IF statement is O(1) for each row.

  • Space Complexity: O(N) in the worst case, to store the result set. This depends on the size of the Employees table. The auxiliary space used by the query itself is constant, O(1).

Code in Different Languages (SQL)

The core logic remains the same across different SQL dialects, but the syntax for conditional statements might vary slightly.

MySQL

SELECT
    employee_id,
    IF(
        employee_id % 2 = 0
        OR SUBSTR(name, 1, 1) = 'M',  -- Using SUBSTR for better compatibility
        0,
        salary
    ) AS bonus
FROM
    employees
ORDER BY employee_id;

Explanation of MySQL Code:

  1. SELECT employee_id, ...: Selects the employee_id column.
  2. IF(condition, value_if_true, value_if_false): A conditional function. The condition checks if the employee_id is even (employee_id % 2 = 0) OR if the first character of the name is 'M' (SUBSTR(name, 1, 1) = 'M'). SUBSTR is used instead of LEFT to ensure better cross-database compatibility.
  3. 0: The bonus if the condition is true (even ID or name starts with 'M').
  4. salary: The bonus if the condition is false (odd ID and name doesn't start with 'M').
  5. AS bonus: Assigns the alias "bonus" to the calculated bonus column.
  6. FROM employees: Specifies the table to query.
  7. ORDER BY employee_id: Orders the results in ascending order of employee_id.

PostgreSQL

SELECT
    employee_id,
    CASE
        WHEN employee_id % 2 = 0 OR SUBSTRING(name FROM 1 FOR 1) = 'M' THEN 0
        ELSE salary
    END AS bonus
FROM
    employees
ORDER BY employee_id;

Explanation of PostgreSQL Code:

PostgreSQL uses CASE statements for conditional logic. The structure and functionality are very similar to the MySQL IF statement. SUBSTRING(name FROM 1 FOR 1) extracts the first character of the name.

SQLite

SELECT
    employee_id,
    CASE
        WHEN employee_id % 2 = 0 OR SUBSTR(name, 1, 1) = 'M' THEN 0
        ELSE salary
    END AS bonus
FROM
    employees
ORDER BY employee_id;

Explanation of SQLite Code:

SQLite's syntax is very similar to PostgreSQL's in this case. It uses CASE statements and SUBSTR for string manipulation.

These SQL queries efficiently solve the problem by directly calculating the bonus within the database query, offering good performance for large datasets. The choice of specific SQL dialect would depend on the database system being used.