Repository navigation
Expand file tree
/
Copy pathProject_1.sql
More file actions
194 lines (145 loc) · 4.01 KB
/
Copy pathProject_1.sql
File metadata and controls
194 lines (145 loc) · 4.01 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
###Data Cleaning
select *
from layoffs;
-- 1. Remove Duplicates
-- 2. Standardize the data
-- 3. Null values or Blank values
-- 4. Remove any columns or rows which is unnecessary
create table layoffs_staging
like layoffs;
Insert into layoffs_staging
select *
from layoffs;
select *
from layoffs_staging;
SELECT COUNT(*) AS total_rows
FROM layoffs_staging;
#############################################################################
###1.Removing Duplicates
select *,
row_number() over (partition by company,location,industry,total_laid_off,
percentage_laid_off,`date`,stage,country,funds_raised_millions) as Row_no
from world_layoffs.layoffs_staging;
with duplicate_val as
(
select *,
row_number() over (partition by company,location,industry,total_laid_off,
percentage_laid_off,`date`,stage,country,funds_raised_millions) as Row_no
from world_layoffs.layoffs_staging
)
select *
from duplicate_val
where Row_no>1;
SELECT COUNT(*) AS total_rows
FROM duplicate_val;
## Again creating a table to remove duplicates
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_no` int
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
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_no
from world_layoffs.layoffs_staging;
select *
from layoffs_staging2;
SELECT COUNT(*) AS total_rows
FROM layoffs_staging2;
select *
from layoffs_staging2
where Row_no>1;
select *
from layoffs_staging2
where company='Casper';
SET SQL_SAFE_UPDATES = 0;
delete
from layoffs_staging2
where Row_no>1;
SET SQL_SAFE_UPDATES = 1;
######################################################################
### Standardize
select distinct funds_raised_millions,trim(funds_raised_millions)
from layoffs_staging2
order by 1;
SET SQL_SAFE_UPDATES = 0;
update layoffs_staging2
set funds_raised_millions = trim(funds_raised_millions);
select distinct country
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*
from layoffs_staging2
where country like '%United States%';
update layoffs_staging2
set country = 'United States'
where country like '%United States%';
select country,trim(trailing '.' from country)
from layoffs_staging2;
update layoffs_staging2
set country = trim(trailing '.' from country);
select `date`,str_to_date(`date`,'%m/%d/%Y')
from layoffs_staging2;
update layoffs_staging2
set `date` = str_to_date(`date`,'%m/%d/%Y');
alter table layoffs_staging2
modify column `date` date;
#######################################################################
###3.Null and blank values
select *
from layoffs_staging2
where industry is null;
select *
from layoffs_staging2
where industry='';
update layoffs_staging2
set industry=null
where industry='';
select t1.industry,t2.industry
from layoffs_staging2 as t1
join layoffs_staging2 as t2
on t1.company = t2.company
where t1.industry is null
and t2.industry is not null;
update layoffs_staging2 as t1
join layoffs_staging2 as 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
where company= 'Airbnb';
#################################################################
###$.Removing unnecessary Row , Column
##Removing row
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;
##Removing Column
select *
from layoffs_staging2;
alter table layoffs_staging2
drop column Row_no;
SELECT COUNT(*) FROM layoffs_staging2;
SELECT COUNT(*) FROM layoffs_staging;
################################################################