-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayoffs_project.sql
More file actions
296 lines (204 loc) · 5.75 KB
/
layoffs_project.sql
File metadata and controls
296 lines (204 loc) · 5.75 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
create database global_job_layoffs;
use global_job_layoffs;
-- duplicate table
CREATE TABLE `layoffs1` (
`company` text,
`location` text,
`industry` text,
`total_laid_off` int DEFAULT NULL,
`percentage_laid_off` text,
`date` text,
`stage` text,
`country` text,
`funds_raised_millions` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
insert into layoffs1
select *
from layoffs;
-- confirm that the duplicate table is populated
select *
from layoffs
where company = 'ola';
-- start data cleaning
-- 1st step remove duplicates
-- identify duplicates
select *, row_number() over(partition by company,location,industry,total_laid_off,percentage_laid_off,'date',stage,country,funds_raised_millions) as detector
from layoffs1;
-- remove duplicates
CREATE TABLE `layoffs2` (
`company` text,
`location` text,
`industry` text,
`total_laid_off` int DEFAULT NULL,
`percentage_laid_off` text,
`date` text,
`stage` text,
`country` text,
`funds_raised_millions` int DEFAULT NULL,
detector int
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
insert into layoffs2
select *, row_number() over(partition by company,location,industry,total_laid_off,percentage_laid_off,'date',stage,country,funds_raised_millions) as detector
from layoffs1;
select *
from layoffs2
where detector >= 2;
delete
from layoffs2
where detector >= 2;
-- drop detector column
alter table layoffs2
drop column detector;
-- trim spaces
select *
from layoffs2;
select trim(company) as company
from layoffs2;
update layoffs2
set funds_raised_millions = trim(funds_raised_millions);
-- set everything null/blank to be null
update layoffs2
set industry = null
where industry = '';
select *
from layoffs2
where country = '';
-- check for wrong spellings and correct
select distinct(industry)
from layoffs2;
update layoffs2
set industry = 'crypto'
where industry like 'crypto%';
update layoffs2
set country = trim(trailing '.' from country)
;
select *
from layoffs2;
-- inspect individual columns and standerdize the data type
select date, str_to_date(date, '%m/%d/%Y') as date
from layoffs2;
update layoffs2
set date = str_to_date(date, '%m/%d/%Y');
select * from layoffs2;
alter table layoffs2
modify column date date;
-- fill the blanks where possible
select industry
from layoffs2
where industry is null;
select *
from layoffs2
where total_laid_off is null;
select *
from layoffs2
where company like '%juul%';
update layoffs2
set industry = 'consumer'
where industry is null and company = "juul";
select *
from layoffs2 as t1
join layoffs2 as t2
on t1.location = t2.location
where (t1.total_laid_off is null and t2.total_laid_off is not null) and (t1.company = t2.company);
update layoffs2 as t1
join layoffs2 as t2
on t1.location = t2.location
set t1.total_laid_off = t2.total_laid_off
where (t1.total_laid_off is null and t2.total_laid_off is not null) and (t1.company = t2.company);
select *
from layoffs2 as t1
join layoffs2 as t2
on t1.location = t2.location
where(t1.percentage_laid_off is null and t2.percentage_laid_off is not null) and (t1.company = t2.company);
update layoffs2 as t1
join layoffs2 as t2
on t1.location = t2.location
set t1.percentage_laid_off = t2.percentage_laid_off
where (t1.percentage_laid_off is null and t2.percentage_laid_off is not null) and (t1.company = t2.company);
select *
from layoffs2
where total_laid_off is null or percentage_laid_off is null;
update layoffs2
set total_laid_off = 0
where total_laid_off is null;
update layoffs2
set percentage_laid_off = 0
where percentage_laid_off is null;
select *
from layoffs2
where company like '2u';
-- delete useless rows in the dataset
select *
from layoffs2
where total_laid_off is null and percentage_laid_off is null;
delete
from layoffs2
where total_laid_off is null and percentage_laid_off is null;
-- Part 2
-- driving insights
select * from layoffs2;
-- find the total laid off
select sum(total_laid_off)
from layoffs2;
-- find total funds raised from tha table
select sum(funds_raised_millions)
from layoffs2;
-- find the distinct industries
select distinct(industry)
from layoffs2;
-- find total funds raised per industry
select industry, sum(funds_raised_millions) as indfund
from layoffs2
group by industry
order by indfund desc;
-- find the company that raised the highest amount of money
select company, sum(funds_raised_millions) as funds
from layoffs2
group by company
order by funds desc
limit 1;
-- find the highest company per industry in funds raised
select company,industry, sum(funds_raised_millions) as funds
from layoffs2
group by company,industry
order by funds desc
limit 1;
-- find the country where companies raise the highest amount of money
select country, sum(funds_raised_millions) as funds
from layoffs2
group by country
order by funds desc
limit 1;
-- find the preffered location of most companies
select location,count(location) preffered
from layoffs2
group by location
order by preffered desc
limit 1;
-- total laid off by industry
select industry,sum(total_laid_off) as laidoff
from layoffs2
group by industry
order by laidoff desc;
-- company that laid off the most workers
select company, sum(total_laid_off) laidoff
from layoffs2
group by company
order by laidoff desc
limit 1;
-- which year did the companies raised the most money
select company,date, max(funds_raised_millions) raised
from layoffs2
group by company,date
order by raised desc
limit 1;
-- list of companies that laid off staff in 2022
select company,date,total_laid_off
from layoffs2
where total_laid_off > 0 and date like "2022%";
-- the countries where healthcare and crypto industries exist
select distinct(country),industry
from layoffs2
where industry like 'health%' or industry like 'crypto'
;
--