-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_cleaning_script_for_the_layoffs.sql
More file actions
243 lines (204 loc) · 6.81 KB
/
data_cleaning_script_for_the_layoffs.sql
File metadata and controls
243 lines (204 loc) · 6.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
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
--- data cleaning script for the layoffs dataset
--- First we insert data into the staging table from the original table
-- This is useful for data validation and testing before moving data to the final table.
-- It allows you to check the data in the staging table before finalizing it.
SELECT *
FROM layoffs;
-- Create Staging Table
CREATE TABLE layoffs_staging
LIKE layoffs;
SELECT *
FROM layoffs_staging;
INSERT layoffs_staging
SELECT *
FROM layoffs;
-- Check the data in the staging table
SELECT * FROM layoffs_staging;
-- Create Final Table
ALTER TABLE layoffs_staging
DROP COLUMN `Source`,
DROP COLUMN `Date Added`;
SELECT * FROM layoffs_staging;
--- Rename Columns and Modify Data Types
ALTER TABLE layoffs_staging
RENAME COLUMN `company` TO `company_name`,
RENAME COLUMN `Location HQ` TO `location`,
RENAME COLUMN `# Laid Off` TO `total_laid_off`,
RENAME COLUMN `Date` TO `date`,
RENAME COLUMN `%` TO `percentage_laid_off`,
RENAME COLUMN `Industry` TO `industry`,
RENAME COLUMN `Stage` TO `stage`,
RENAME COLUMN `$ Raised (mm)` TO `funds_raised_millions`,
RENAME COLUMN `Country` TO `country`;
ALTER TABLE layoffs_staging
MODIFY COLUMN `company_name` VARCHAR(255),
MODIFY COLUMN `location` VARCHAR(255),
MODIFY COLUMN `total_laid_off` INT,
MODIFY COLUMN `date` VARCHAR(255),
MODIFY COLUMN `percentage_laid_off` VARCHAR(255),
MODIFY COLUMN `industry` VARCHAR(255),
MODIFY COLUMN `stage` VARCHAR(255),
MODIFY COLUMN `funds_raised_millions` VARCHAR(255),
MODIFY COLUMN `country` VARCHAR(255);
-- checking duplicate values in the staging table
--🔍 Step 1: Use a CTE to tag duplicates
WITH duplicates AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY company_name, location, total_laid_off, date, industry, stage, funds_raised_millions, country
ORDER BY row_num
) AS rn
FROM layoffs_staging
)
-- 🔍 Select duplicates
SELECT *
FROM duplicates
WHERE rn > 1;
--we ran into issues with ctes so we Create the layoffs_staging2 table with the same structure as layoffs_staging
CREATE TABLE `layoffs_staging2` (
`company_name` VARCHAR(255) DEFAULT NULL,
`location` VARCHAR(255) DEFAULT NULL,
`industry` VARCHAR(255) DEFAULT NULL,
`total_laid_off` INT DEFAULT NULL,
`percentage_laid_off` VARCHAR(255) DEFAULT NULL,
`date` VARCHAR(255) DEFAULT NULL,
`stage` VARCHAR(255) DEFAULT NULL,
`country` VARCHAR(255) DEFAULT NULL,
`funds_raised_millions` VARCHAR(255) DEFAULT NULL,
`row_num` INT
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
SELECT * FROM layoffs_staging2;
-- Insert relevant columns and add row_num for identifying duplicates
-- insert the columns and add the row_num for each row
INSERT INTO layoffs_staging2 (
company_name, location, industry, total_laid_off, percentage_laid_off, date, stage, country, funds_raised_millions, row_num
)
SELECT
company_name,
location,
industry,
total_laid_off,
percentage_laid_off,
`date`,
stage,
country,
funds_raised_millions,
ROW_NUMBER() OVER (
PARTITION BY company_name, location, industry, total_laid_off, percentage_laid_off, `date`, stage, country, funds_raised_millions
ORDER BY company_name
) AS row_num
FROM layoffs_staging;
---check the duplicates
SELECT *
FROM layoffs_staging2
WHERE row_num > 1;
---- Delete duplicates from the staging table
DELETE FROM layoffs_staging2
WHERE row_num > 1;
SELECT *
FROM layoffs_staging2;
-- Turn date column into date datatype
SELECT `date`
FROM layoffs_staging2;
SELECT `date`
FROM layoffs_staging2;
---convert it into a proper MySQL DATE (YYYY-MM-DD) format.
---test before updating
SELECT `date`, STR_TO_DATE(`date`, '%m/%d/%Y') AS converted_date
FROM layoffs_staging2;
---Update the table with properly converted DATE values
UPDATE layoffs_staging2
SET `date` = STR_TO_DATE(`date`, '%m/%d/%Y');
--- Change the column to DATE datatype
ALTER TABLE layoffs_staging2
MODIFY COLUMN `date` DATE;
SELECT DISTINCT industry
FROM layoffs_staging2
ORDER BY 1;
-- Remove rows with no value to us; in this case, where the relevant values are NULL
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
WHERE NOT country = 'United States';
--- Check for non-U.S. companies with "Non-U.S." in the location column
SELECT company_name, location, country
FROM layoffs_staging2
WHERE country != 'United States'
AND LOWER(location) NOT LIKE '%non-u.s.%';
--- Add "Non-U.S." to the location for non-U.S. companies
UPDATE layoffs_staging2
SET location = CONCAT(location, ', Non-U.S.')
WHERE country != 'United States'
AND LOWER(location) NOT LIKE '%non-u.s.%';
--- Check for NULL values in the industry column
--- If NULL, set to 'Other'
UPDATE layoffs_staging2
SET industry = 'Other'
WHERE industry IS NULL;
SELECT * FROM layoffs_staging2;
select COUNT (*) as totalnulls
FROM layoffs_staging2
WHERE total_laid_off IS NULL
AND percentage_laid_off IS NULL;
---Check for trailing periods in the country column
SELECT *
FROM layoffs_staging2
WHERE country LIKE '%.';
---- Check for trailing periods in the country column
SELECT DISTINCT country, TRIM(TRAILING '.' FROM country)
FROM layoffs_staging2
ORDER BY 1;
--- update for trailing periods in the country column
UPDATE layoffs_staging2
SET country = TRIM(TRAILING '.' FROM country)
WHERE country LIKE '%.%';
--- Check for trailing periods in the company_name column
SELECT company_name, TRIM(company_name)
FROM layoffs_staging2;
---- update company_name column with trailing periods
UPDATE layoffs_staging2
SET company_name = TRIM(company_name)
WHERE company_name LIKE '%.';
---clean the trailing dot (.) from such entries
SELECT *
FROM layoffs_staging2
WHERE company_name LIKE '%.';
---updating entries with trailing dots (.)
UPDATE layoffs_staging2
SET company_name = TRIM(TRAILING '.' FROM company_name)
WHERE company_name LIKE '%.';
SELECT funds_raised_millions
FROM layoffs_staging2
ORDER BY 1 DESC;
UPDATE layoffs_staging2
SET funds_raised_millions = NULL
WHERE funds_raised_millions = 'NULL';
--- Check for non-numeric values in the funds_raised_millions column
UPDATE layoffs_staging2
SET funds_raised_millions = REPLACE(REPLACE(funds_raised_millions, '$', ''), ',', '')
WHERE funds_raised_millions IS NOT NULL;
---Convert the column to INT datatype
ALTER TABLE layoffs_staging2
MODIFY COLUMN funds_raised_millions INT;
-- Remove Any Columns
---I'm doing analysis that requires numeric layoff counts, or
---I want to work only with complete and reliable data.
DELETE FROM layoffs_staging2
WHERE total_laid_off IS NULL
AND percentage_laid_off IS NOT NULL
AND funds_raised_millions IS NOT NULL;
DELETE FROM layoffs_staging2
WHERE total_laid_off IS NULL
AND percentage_laid_off IS NOT NULL
AND funds_raised_millions IS NULL;
ALTER TABLE layoffs_staging2
DROP COLUMN row_num;
SELECT *
FROM layoffs_staging2;