-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path577_Employee_Bonus.sql
53 lines (42 loc) · 1.92 KB
/
577_Employee_Bonus.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- Source: https://leetcode.com/problems/employee-bonus/?envType=study-plan-v2&envId=top-sql-50
-- Table: Employee
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | empId | int |
-- | name | varchar |
-- | supervisor | int |
-- | salary | int |
-- +-------------+---------+
-- empId is the column with unique values for this table.
-- Each row of this table indicates the name and the ID of an employee in addition to their salary and the id of their manager.
-- Table: Bonus
-- +-------------+------+
-- | Column Name | Type |
-- +-------------+------+
-- | empId | int |
-- | bonus | int |
-- +-------------+------+
-- empId is the column of unique values for this table.
-- empId is a foreign key (reference column) to empId from the Employee table.
-- Each row of this table contains the id of an employee and their respective bonus.
-- 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.
------------------------------------------------------------------------------
-- SQL Schema
Create table If Not Exists Employee (empId int, name varchar(255), supervisor int, salary int)
Create table If Not Exists Bonus (empId int, bonus int)
Truncate table Employee
insert into Employee (empId, name, supervisor, salary) values ('3', 'Brad', 'None', '4000')
insert into Employee (empId, name, supervisor, salary) values ('1', 'John', '3', '1000')
insert into Employee (empId, name, supervisor, salary) values ('2', 'Dan', '3', '2000')
insert into Employee (empId, name, supervisor, salary) values ('4', 'Thomas', '3', '4000')
Truncate table Bonus
insert into Bonus (empId, bonus) values ('2', '500')
insert into Bonus (empId, bonus) values ('4', '2000')
-- MS SQL Server Code
SELECT e.name, b.bonus
FROM Employee e
LEFT JOIN Bonus b
ON e.empId = b.empID
WHERE ISNULL(b.bonus,0) < 1000