-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL_Code--Covid-19_Analysis.sql
294 lines (246 loc) · 6.42 KB
/
SQL_Code--Covid-19_Analysis.sql
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
# Covid-19 Analysis
# 1.Total cases v/s total deaths
# Death percentage
SELECT
date,
total_cases,
total_deaths,
ROUND((total_deaths / total_cases)*100 ,2) AS death_percentage
FROM
covid_deaths
WHERE
total_cases AND total_deaths IS NOT NULL
ORDER BY 1,2;
# 2.Total cases v/s Population Infection_percentage
SELECT
location,
date,
population,
total_cases,
ROUND((total_cases / population)*100 ,4) AS Infection_percentage
FROM
covid_deaths
WHERE
total_cases AND total_deaths IS NOT NULL
ORDER BY 1,2;
# 3.Countries with Highest Infection_percentage to Population
SELECT
location,
population,
MAX(total_cases) AS Highest_Infection_Rate,
ROUND(MAX(total_cases / population)*100 ,2) AS Maximum_Infection_percentage
FROM
covid_deaths
WHERE
total_cases AND total_deaths IS NOT NULL
GROUP BY
location,
population
HAVING
Maximum_Infection_percentage <>0
ORDER BY
Maximum_Infection_percentage DESC;
# 4.Countries with Highest Death Count per Population
SELECT
location,
MAX(CAST(total_deaths AS UNSIGNED)) AS Total_deaths
FROM
covid_deaths
WHERE
continent IS NOT NULL
GROUP BY
location
HAVING
Total_deaths <> 0
ORDER BY
Total_deaths DESC;
# 5.Continents with Highest Death Count per Population
SELECT
continent,
MAX(CAST(total_deaths AS UNSIGNED)) AS Total_deaths
FROM
covid_deaths
WHERE
continent IS NOT NULL
GROUP BY
continent
ORDER BY
Total_deaths DESC;
# 6.Global statistical description
SELECT
SUM(new_cases) AS Total_cases,
SUM(new_deaths) AS Total_deaths,
ROUND((SUM(new_deaths) / SUM(new_cases) * 100),2) AS Death_percentage
FROM
covid_deaths
WHERE
continent IS NOT NULL
ORDER BY
1,2;
# 7. Total Popualtion v/s Vaccinations
SELECT
deths.continent,
deths.location,
deths.date,
deths.population,
vacs.new_vaccinations
FROM
covid_deaths deths
JOIN
covid_vaccinations vacs
ON deths.location = vacs.location AND
deths.date = vacs.date
WHERE deths.continent IS NOT NULL
HAVING vacs.new_vaccinations IS NOT NULL
ORDER BY 2,3;
# 8.Count Vaccination done over continent
SELECT
deths.continent,
deths.location,
deths.date,
deths.population,
vacs.new_vaccinations,
SUM(CONVERT(vacs.new_vaccinations, UNSIGNED))
OVER (PARTITION BY deths.location ORDER BY deths.location, deths.date) AS Rolling_sum_of_vaccination
FROM
covid_deaths deths
JOIN
covid_vaccinations vacs
ON deths.location = vacs.location AND
deths.date = vacs.date
WHERE
deths.continent IS NOT NULL
ORDER BY
deths.location, deths.date;
# 9. Vaccination Percentage by continent
WITH cte AS
(
SELECT
deths.continent,
deths.location,
deths.date,
deths.population,
vacs.new_vaccinations,
SUM(CONVERT(vacs.new_vaccinations, UNSIGNED))
OVER (PARTITION BY deths.location ORDER BY deths.location, deths.date) AS Rolling_sum_of_vaccination
FROM
covid_deaths deths
JOIN
covid_vaccinations vacs
ON deths.location = vacs.location AND
deths.date = vacs.date
WHERE
deths.continent IS NOT NULL
)
SELECT *, ( Rolling_sum_of_vaccination / population)*100 AS Percentage
FROM cte
HAVING Percentage IS NOT NULL ;
# View for Tableau visualisation
# 1. Total cases, Total deaths and Death_percentage
SELECT
SUM(new_cases) AS Total_cases,
SUM(new_deaths) AS Total_deaths,
ROUND((SUM(new_deaths) / SUM(new_cases) * 100),2) AS Death_percentage
FROM
covid_deaths
WHERE
continent IS NOT NULL;
# 2. Death count by location
Select location, SUM(new_deaths) as TotalDeathCount
From covid_deaths
Where continent is null
and location not in ('World', 'European Union', 'International')
Group by location
order by TotalDeathCount desc;
# 3 Location wise Population,HighestInfectionCount and PercentPopulationInfected
Select Location, Population, MAX(total_cases) as HighestInfectionCount, Max((total_cases/population))*100 as PercentPopulationInfected
From covid_deaths
Group by Location, Population
order by PercentPopulationInfected desc;
# 4
Select Location, Population,date, MAX(total_cases) as HighestInfectionCount, Max((total_cases/population))*100 as PercentPopulationInfected
From covid_deaths
Group by Location, Population, date
order by PercentPopulationInfected desc;
# 5.Top 10 Countries with Highest Death Count per Population
SELECT
location,
MAX(CAST(total_deaths AS UNSIGNED)) AS Total_deaths
FROM
covid_deaths
WHERE
continent IS NOT NULL
GROUP BY
location
HAVING
Total_deaths <> 0
ORDER BY
Total_deaths DESC
LIMIT 10;
#6. Tests Analysis
SELECT
SUM(new_tests) AS New_Tests,
SUM(total_tests) AS Total_tests,
ROUND((SUM(new_tests) / SUM(total_tests) * 100),2) AS Test_percentage
FROM
covid_vaccinations
WHERE
continent IS NOT NULL;
# 7. Total Vaccinations by continent
SELECT
deths.continent,
SUM(vacs.new_vaccinations) AS Vaccinations
FROM
covid_deaths deths
JOIN
covid_vaccinations vacs
ON deths.location = vacs.location AND
deths.date = vacs.date
WHERE deths.continent IS NOT NULL
GROUP BY deths.continent
ORDER BY Vaccinations DESC;
# 8. Vaccination Percentage by continent
WITH cte AS
(
SELECT
deths.continent,
deths.location,
deths.date,
deths.population,
vacs.new_vaccinations,
SUM(CONVERT(vacs.new_vaccinations, UNSIGNED))
OVER (PARTITION BY deths.location ORDER BY deths.location, deths.date) AS Rolling_sum_of_vaccination
FROM
covid_deaths deths
JOIN
covid_vaccinations vacs
ON deths.location = vacs.location AND
deths.date = vacs.date
WHERE
deths.continent IS NOT NULL
)
SELECT continent,date, population, ( Rolling_sum_of_vaccination / population)*100 AS Percentage
FROM cte
HAVING Percentage IS NOT NULL ;
# 9. Top 10 Countries with Highest Vaccination Count per Population
SELECT
vacs.location,
SUM(vacs.new_vaccinations) AS Vaccinations
FROM
covid_deaths deths
JOIN
covid_vaccinations vacs
ON deths.location = vacs.location AND
deths.date = vacs.date
WHERE deths.continent IS NOT NULL
GROUP BY vacs.location
ORDER BY Vaccinations DESC
LIMIT 10;
# 10. Top 10 countries by Average positive_rate
SELECT location,ROUND(AVG(positive_rate),2) AS Average_positiverate
FROM covid_vaccinations
GROUP BY location
HAVING Average_positiverate IS NOT NULL
ORDER BY Average_positiverate DESC
LIMIT 10;
-- ------------------------------------------------------------------------------------------------------------------------------------------