-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathData Cleaning.sql
More file actions
208 lines (152 loc) · 3.85 KB
/
Data Cleaning.sql
File metadata and controls
208 lines (152 loc) · 3.85 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
-- Data cleaning project 1 includes
-- 1. remove duplicates
-- 2. standardized the data
-- 3. manage null or blank values
-- 4. remove any columns
select*
from layoffs;
-- to preserve the raw data, work on a copy of the raw data
create table layoffs_staging
like layoffs;
select *
from layoffs_staging;
insert layoffs_staging
select *
from layoffs;
-- REMOVE ANY DUPLICATES
select *,
row_number() over(
partition by company, industry, total_laid_off, percentage_laid_off, `date`) AS row_num
from layoffs_staging;
With duplicate_cte AS
(
select *,
ROW_NUMBER() OVER(
PARTITION BY company, location, industry, total_laid_off, percentage_laid_off, `date`, stage, country, funds_raised_millions) AS row_num
from layoffs_staging
)
SELECT *
FROM duplicate_cte
WHERE row_num > 1;
With duplicate_cte AS
(
select *,
ROW_NUMBER() OVER(
PARTITION BY company, location, industry, total_laid_off, percentage_laid_off, `date`, stage, country, funds_raised_millions) AS row_num
from layoffs_staging
)
DELETE
FROM duplicate_cte
WHERE row_num > 1;
CREATE TABLE `layoffs_staging2` (
`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,
`row_num` int
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
select *
from layoffs_staging2;
insert into layoffs_staging2
select *,
ROW_NUMBER() OVER(
PARTITION BY company, location, industry, total_laid_off, percentage_laid_off, `date`, stage, country, funds_raised_millions) AS row_num
from layoffs_staging;
select *
from layoffs_staging2
where row_num > 1;
insert into layoffs_staging2
select *,
ROW_NUMBER() OVER(
PARTITION BY company, location, industry, total_laid_off, percentage_laid_off, `date`, stage, country, funds_raised_millions) AS row_num
from layoffs_staging;
select *
FROM layoffs_staging2
where row_num > 1;
delete
FROM layoffs_staging2
where row_num > 1;
select *
from layoffs_staging2;
-- STANDARDIZING DATA
select company, TRIM(company)
from layoffs_staging2;
update layoffs_staging2
set company = TRIM(company);
select distinct industry
from layoffs_staging2
order by 1;
select *
from layoffs_staging2
where industry like 'crypto%';
update layoffs_staging2
set industry = 'crypto'
where industry like 'crypto%';
select distinct country, trim(trailing '.' from country)
from layoffs_staging2
order by 1;
update layoffs_staging2
set country = trim(trailing '.' from country)
where country like 'united states%';
select `date`
from layoffs_staging2;
select `date`,
str_to_date(`date`, '%m/%d/%Y')
from layoffs_staging2;
alter table layoffs_staging2
modify column `date` date;
update layoffs_staging2
set `date` = str_to_date(`date`, '%m/%d/%Y');
alter table layoffs_staging2
modify column `date` date;
select *
from layoffs_staging2;
-- Handling NULL or BLANK values
select *
from layoffs_staging2
where total_laid_off is NULL
AND percentage_laid_off is null;
update layoffs_staging2
set industry = null
where industry = '';
select *
from layoffs_staging2;
select *
from layoffs_staging2
where industry is null
or industry = '';
select *
from layoffs_staging2
where company = 'Airbnb';
select t1.industry, t2.industry
from layoffs_staging2 t1
join layoffs_staging2 t2
on t1.company = t2.company
where (t1.industry is null or t1.industry = '')
AND t2.industry is not null;
update layoffs_staging2 t1
join layoffs_staging2 t2
on t1.company = t2.company
set t1.industry = t2.industry
where t1.industry is null
and t2.industry is not null;
select *
from layoffs_staging2;
-- REMOVE COLUMNS AND ROWS
select *
from layoffs_staging2
where total_laid_off is NULL
AND percentage_laid_off is null;
delete
from layoffs_staging2
where total_laid_off is NULL
AND percentage_laid_off is null;
select *
from layoffs_staging2;
alter table layoffs_staging2
drop column row_num;