-
Notifications
You must be signed in to change notification settings - Fork 43
/
employees-with-missing-information.sql
158 lines (140 loc) · 3.45 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
1965. Employees With Missing Information
Level
Easy
Description
Table: Employees
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| employee_id | int |
| name | varchar |
+-------------+---------+
employee_id is the primary key for this table.
Each row of this table indicates the name of the employee whose ID is employee_id.
Table: Salaries
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| employee_id | int |
| salary | int |
+-------------+---------+
employee_id is the primary key for this table.
Each row of this table indicates the salary of the employee whose ID is employee_id.
Write an SQL query to report the IDs of all the employees with missing information. The information of an employee is missing if:
The employee’s name is missing, or
The employee’s salary is missing.
Return the result table ordered by employee_id in ascending order.
The query result format is in the following example:
Employees table:
+-------------+----------+
| employee_id | name |
+-------------+----------+
| 2 | Crew |
| 4 | Haven |
| 5 | Kristian |
+-------------+----------+
Salaries table:
+-------------+--------+
| employee_id | salary |
+-------------+--------+
| 5 | 76071 |
| 1 | 22517 |
| 4 | 63539 |
+-------------+--------+
Result table:
+-------------+
| employee_id |
+-------------+
| 1 |
| 2 |
+-------------+
Employees 1, 2, 4, and 5 are working at this company.
The name of employee 1 is missing.
The salary of employee 2 is missing.
*/
# V0
select e.employee_id
from Employees e
left join Salaries s
ON s.employee_id = e.employee_id
where s.salary is null
union all
select s.employee_id
from Employees e
right join Salaries s
ON s.employee_id = e.employee_id
where e.name is null
order by employee_id;
# V1
# https://leetcode.ca/2021-08-16-1965-Employees-With-Missing-Information/
# Write your MySQL query statement below
select e.employee_id
from Employees e
left join Salaries s
using(employee_id)
where salary is null
union all
select s.employee_id
from Employees e
right join Salaries s
using(employee_id)
where name is null
order by employee_id;
# V2
# 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;
# V2'
# 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;
# V2''
# 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;