-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-employee-bonus.sql
More file actions
36 lines (32 loc) · 1.17 KB
/
07-employee-bonus.sql
File metadata and controls
36 lines (32 loc) · 1.17 KB
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
-- ============================================
-- 7. Employee Bonus
-- LeetCode #577
-- ============================================
-- Problem: Report the name and bonus amount of each employee with a bonus less than 1000.
-- Include employees who have no bonus record (NULL bonus).
-- Table: Employee
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | empId | int |
-- | name | varchar |
-- | supervisor | int |
-- | salary | int |
-- +-------------+---------+
-- Table: Bonus
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | empId | int |
-- | bonus | int |
-- +-------------+---------+
-- Solution: LEFT JOIN + filter for bonus < 1000 OR NULL
SELECT e.name, b.bonus
FROM Employee e
LEFT JOIN Bonus b ON e.empId = b.empId
WHERE b.bonus < 1000 OR b.bonus IS NULL;
-- Explanation:
-- LEFT JOIN keeps all employees even if they have no bonus record.
-- Employees without a bonus will have NULL in the bonus column.
-- We filter for bonus < 1000 OR bonus IS NULL.
-- Important: NULL < 1000 evaluates to NULL (not TRUE), so we must explicitly check IS NULL.