Description
Table: Employee
| Column Name | Type |
|---|---|
| empId | int |
| name | varchar |
| supervisor | int |
| salary | int |
empIdis the column with unique values for this table.- Each row of this table indicates the
nameand the ID of an employee in addition to theirsalaryand the id of their manager.
Table: Bonus
| Column Name | Type |
|---|---|
| empId | int |
| bonus | int |
empIdis the column of unique values for this table.empIdis a foreign key (reference column) toempIdfrom theEmployeetable.- Each row of this table contains the id of an employee and their respective
bonus.
Problem Statement
Write a solution to report the name and bonus amount of each employee with a bonus less than 1000.
Return the result table in any order. The result format is in the following example.
Example 1:
Input:
Employeetable:
| empId | name | supervisor | salary |
|---|---|---|---|
| 3 | Brad | null | 4000 |
| 1 | John | 3 | 1000 |
| 2 | Dan | 3 | 2000 |
| 4 | Thomas | 3 | 4000 |
Bonustable:
| empId | bonus |
|---|---|
| 2 | 500 |
| 4 | 2000 |
Output:
| name | bonus |
|---|---|
| Brad | null |
| John | null |
| Dan | 500 |
Solution
In this case, we need to return two fields in the output: name and bonus of an employee.
The problem can be divided into following parts:
- Find
empIdfromBonustable who have less than 1000 as bonus amount. - Associate
nameto these employees usingEmployeetable. You will have to perform join in this case. - One caveat with this problem is that if the employee is not present in
Bonustable, that employee had 0 as bonus amount. So, in order to consider this, you either have to mark those employee bonus as 0 or you can use performEmployee LEFT OUTER JOIN Bonusto retrieve all employees and not just employees present in theBonustable.
1SELECT e.name, b.bonus
2 FROM Employee e
3 LEFT OUTER JOIN Bonus b
4 ON e.empId = b.empId
5 WHERE b.bonus IS NULL OR b.bonus < 1000;


Comments