forked from black-shadows/LeetCode-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemployees-with-missing-information.sql
60 lines (54 loc) · 1.17 KB
/
employees-with-missing-information.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
54
55
56
57
58
59
60
# Time: O(nlogn)
# Space: O(n)
WITH all_employee_info_cte AS
(
SELECT employee_id FROM Employees
UNION ALL
SELECT employee_id FROM Salaries
)
SELECT employee_id
FROM all_employee_info_cte
GROUP BY employee_id
HAVING COUNT(*) != 2
ORDER BY 1;
# Time: O(nlogn)
# Space: O(n)
WITH all_employee_id_cte AS
(
SELECT employee_id FROM Employees
UNION
SELECT employee_id FROM Salaries
),
complete_employee_id_cte AS
(
SELECT a.employee_id
FROM Employees a
INNER JOIN Salaries b
ON a.employee_id = b.employee_id
)
SELECT employee_id
FROM all_employee_id_cte a
WHERE NOT EXISTS (SELECT 1 FROM complete_employee_id_cte b WHERE a.employee_id = b.employee_id)
ORDER BY 1;
# Time: O(nlogn)
# Space: O(n)
WITH all_employee_info_cte AS
(
(
SELECT a.employee_id, a.name, b.salary
FROM Employees a
LEFT JOIN Salaries b
ON a.employee_id = b.employee_id
)
UNION
(
SELECT b.employee_id, a.name, b.salary
FROM Employees a
RIGHT JOIN Salaries b
ON a.employee_id = b.employee_id
)
)
SELECT employee_id
FROM all_employee_info_cte tmp
WHERE name IS NULL OR salary IS NULL
ORDER BY 1;