-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatacleaning.sql
More file actions
297 lines (198 loc) · 4.61 KB
/
datacleaning.sql
File metadata and controls
297 lines (198 loc) · 4.61 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
293
294
295
296
297
-- DATA CLEANING
-- WORLD LAYOFFS DATA
-- selecting everything from layoffs table
select *
from layoffs;
-- selecting date from layoffs table
select layoff_date
from layoffs;
-- converting date to YYYY-MM-DD format
select
layoff_date,
substr(layoff_date, instr(layoff_date, '/') + 1) AS rest,
date(
substr(layoff_date, -4) || '-' ||
printf('%02d', substr(layoff_date, 1, instr(layoff_date, '/') - 1)) || '-' ||
printf(
'%02d',
substr(
substr(layoff_date, instr(layoff_date, '/') + 1),
1,
instr(substr(layoff_date, instr(layoff_date, '/') + 1), '/') - 1
))) as converted_date
from layoffs;
-- updating the date format
update layoffs
set layoff_date = date(
substr(layoff_date, -4) || '-' ||
printf('%02d', substr(layoff_date, 1, instr(layoff_date, '/') - 1)) || '-' ||
printf(
'%02d',
substr(
substr(layoff_date, instr(layoff_date, '/') + 1),
1,
instr(substr(layoff_date, instr(layoff_date, '/') + 1), '/') - 1
)))
where layoff_date like '%/%/%';
-- checking the result
select *
from layoffs;
-- updating the null text to null
UPDATE layoffs_staging2
SET layoff_date = NULL
WHERE layoff_date = 'NULL';
-- creating a staging table
create table layoffs_staging as
select *
from layoffs
where 1 = 0;
-- checking the result
pragma table_info(layoffs_staging);
-- inserting data into the staging table
insert into layoffs_staging
select *
from layoffs;
-- identifying the duplicate row with CTE and window function
with duplicate_cte as(
select *,
ROW_NUMBER() over(
partition by company,
location,
industry,
total_laid_off,
percentage_laid_off,
layoff_date,
stage,
country,
funds_raised_millions) as rownum
from layoffs_staging
)
select * from duplicate_cte where rownum>1;
--checking the result
select *
from layoffs_staging
where company
like "Casper";
-- creating a second staging table
create table
layoffs_staging2 as
select *
from layoffs_staging
where 1=0;
-- adding a row rownum for the count of duplicate rows
alter table layoffs_staging2
add column rownum INTEGER;
-- insering into staging table
insert into layoffs_staging2
select *,
ROW_NUMBER() over(
partition by company,
location,
industry,
total_laid_off,
percentage_laid_off,
layoff_date,
stage,
country,
funds_raised_millions) as rownum
from layoffs_staging;
-- selecting the duplicate rows
select *
from layoffs_staging2
where rownum>1;
-- deleting the duplicates
delete
from layoffs_staging2
where rownum>1;
-- trimming the company name
select
distinct trim(company)
from layoffs_staging2;
-- updating company name with trimmed name
update layoffs_staging2
set company = trim(company);
-- updating industry name with trimmed value
update layoffs_staging2
set industry = trim(industry);
-- looking for same industry with distinct name
select distinct industry
from layoffs_staging2
order by industry;
-- crypto industry have multiple names
select *
from layoffs_staging2
where industry
like "Crypto%";
-- updating the crypto industry with name "Crypto"
update layoffs_staging2
set industry = "Crypto"
where industry like "Crypto%";
-- looking for same location names
select
distinct location
from layoffs_staging2
order by 1;
-- looking for same country names
select
distinct country
from layoffs_staging2
order by 1;
-- identified same country name
select
distinct country,
trim(country,".")
from layoffs_staging2
where country
like "United States%";
-- updating the country name
update layoffs_staging2
set country = trim(country,".")
where country
like "United States%";
-- looking for null industry name
select *
from layoffs_staging2
where industry is null
or industry = "";
-- looking for industry names for null values
select
t1.company,
t1.location,
t1.industry,
t2.industry
from layoffs_staging2 t1
join layoffs_staging2 t2
on t1.company = t2.company
where t1.industry is null
and t2.industry is not null;
-- updating the null industry names with corresponding industry names
update layoffs_staging2
set industry = (
select t2.industry
from layoffs_staging2 t2
where t2.company = layoffs_staging2.company
and t2.industry is not null
limit 1
)
where industry is null
and company in (
select company
from layoffs_staging2
where industry is not null
);
-- looking for useless rows with has null values
select *
from layoffs_staging2
where total_laid_off is null
and percentage_laid_off is null;
-- deleting the useless rows
delete from
layoffs_staging2
where total_laid_off is null
and percentage_laid_off is null;
-- removing the rownum column from the table as it is has no more use
alter table layoffs_staging2
drop column rownum;
-- final table with is clean
select *
from layoffs_staging2;