-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData Cleaning.sql
More file actions
193 lines (148 loc) · 3.81 KB
/
Data Cleaning.sql
File metadata and controls
193 lines (148 loc) · 3.81 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
USE world_layoffs;
SELECT *
FROM layoffs;
-- 1. Remove Duplicates
-- 2. Standardize the Data
-- 3. Null Values or blank values
-- 4. Remove Any Columns or Rows
-- Creating a staging table --
CREATE TABLE layoffs_staging
LIKE layoffs;
SELECT *
FROM layoffs_staging;
INSERT layoffs_staging
SELECT *
FROM layoffs;
-- Identifying Duplicate Values for all columns --
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;
SELECT *
FROM layoffs_staging
WHERE company = "Casper";
-- Creating another staging table with row_num column added to delete duplicate rows --
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;
DELETE
FROM layoffs_staging2
WHERE row_num > 1;
SELECT *
FROM layoffs_staging2;
-- Standardizing data --
SELECT DISTINCT(TRIM(company))
FROM layoffs_staging2;
UPDATE layoffs_staging2
SET company = TRIM(company);
SELECT DISTINCT(company)
FROM layoffs_staging2;
SELECT DISTINCT(industry)
FROM layoffs_staging2;
SELECT *
FROM layoffs_staging2
WHERE industry LIKE 'Crypto%';
UPDATE layoffs_staging2
SET industry = 'Crypto'
WHERE industry LIKE 'Crypto%';
SELECT *
FROM layoffs_staging2;
SELECT DISTINCT country
FROM layoffs_staging2
ORDER BY 1;
SELECT DISTINCT country
FROM layoffs_staging2
WHERE country
LIKE 'United Sta%';
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`,
str_to_date(`date`, '%m/%d/%Y')
FROM layoffs_staging2;
UPDATE layoffs_staging2
SET `date` = str_to_date(`date`, '%m/%d/%Y');
SELECT `date`
FROM layoffs_staging2;
ALTER TABLE layoffs_staging2
MODIFY COLUMN `date` DATE;
SELECT *
FROM layoffs_staging2;
-- Removing or Populating null values --
SELECT *
FROM layoffs_staging2
WHERE total_laid_off IS NULL
AND percentage_laid_off IS NULL;
SELECT *
FROM layoffs_staging2
WHERE industry IS NULL
OR industry = '';
SELECT *
FROM layoffs_staging2
WHERE company = 'Juul';
SELECT t1.company, t2.company, t1.industry, t2.industry
FROM layoffs_staging2 t1
left JOIN layoffs_staging2 t2
ON t1.company = t2.company
WHERE ( t1.industry = '')
AND t2.industry IS NOT NULL;
UPDATE layoffs_staging2 t1
JOIN layoffs_staging t2
ON t1.company = t2.company
SET t1. industry = t2. industry
WHERE (t1.industry IS NULL OR t1.industry = '')
AND t2. industry IS NOT NULL;
UPDATE layoffs_staging2
SET industry = NULL
WHERE industry = '';
with t1 as (SELECT *
FROM layoffs_staging2
WHERE industry IS NULL OR industry = ''),
t2 as (SELECT *
FROM layoffs_staging2
WHERE industry IS not NULL)
SELECT t1.company, t1.industry, t2.company, t2.industry
FROM t1 JOIN t2
ON t1.company = t2.company;
-- checking null values in total_laid_off and percentage_laid_off columns--
SELECT *
FROM layoffs_staging2
WHERE total_laid_off IS NULL
AND percentage_laid_off IS NULL;
-- deleting these rows since both columns are empty and therefore unusable --
DELETE
FROM layoffs_staging2
WHERE total_laid_off IS NULL
AND percentage_laid_off IS NULL;
-- Removing column row_num --
SELECT *
FROM layoffs_staging2;
ALTER TABLE layoffs_staging2
DROP COLUMN row_num;