-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCovid Project.sql
315 lines (231 loc) · 7.55 KB
/
Covid Project.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
Covid 19 Data Exploration
Skills used: Joins, Aggregate Functions, Converting/Casting data types, CTE's, Window Functions, Temp Tables, Creating Views
*/
-- This query displays the CovidDeath Table
SELECT *
FROM CovidDeaths
WHERE continent is not null
ORDER BY 3,4
-- This query displays the CovidVaccinations Table
SELECT *
FROM CovidVaccinations
ORDER BY 3,4
-- This shows the columns we are working with
SELECT location,
date,
total_cases,
new_cases,
total_deaths,
population
FROM CovidDeaths
ORDER BY 1,2
total deaths started ramping up amonth after the initial case was found.
-- Lets look at the Percentage of Total Deaths vs Total Cases in Nigeria
SELECT location,
date,
total_cases,
total_deaths,
(total_deaths/total_cases)* 100 as death_percentage
FROM CovidDeaths
WHERE location in ('Nigeria') and continent is not null
ORDER BY 1,2
-- Lets look at the Percentage of Total Cases vs Population in Nigeria
SELECT location,
date,
total_cases,
population,
(total_cases/population)*100 as covid_percentage_per_population
FROM CovidDeaths
WHERE location in ('Nigeria')
ORDER BY total_cases
-- Lets look at Countries with the Highest Infection Rate compared to Population
SELECT location,
population,
MAX(total_cases) as highest_infection_count,
MAX((total_cases/population))* 100 as percentage_infected_population
FROM CovidDeaths
WHERE continent is not null
GROUP BY location, population
ORDER BY percentage_infected_population desc
-- Lets look at Countires with the Highest Death Rate compared to Population
SELECT location,
population,
MAX(cast(total_deaths as int)) as highest_death_rate,
MAX((total_deaths/population)) *100 as percentage_death_population
FROM CovidDeaths
--WHERE continent is not null
GROUP BY location, population
ORDER BY highest_death_rate desc
-- Lets take a look at the CONTINENTS
-- Lets look at Continents with the Highest Death Rate compared to Population
SELECT continent,
MAX(cast(total_deaths as int)) as highest_death_rate
FROM CovidDeaths
WHERE continent is not null
GROUP BY continent
ORDER BY highest_death_rate desc
-- An Alternative way to look at it
SELECT location,
MAX(cast(total_deaths as int)) as highest_death_rate
FROM CovidDeaths
WHERE continent is null
GROUP BY location
ORDER BY highest_death_rate desc
--LETS LOOK AT GLOBAL NUMBERS
SELECT date,
SUM(new_cases) as total_cases,
SUM(cast(new_deaths as int)) as total_deaths,
SUM(cast(new_deaths as int))/SUM(New_cases) * 100 as death_percentage
FROM CovidDeaths
WHERE continent is not null
GROUP BY date
ORDER BY 1,2
--WITHOUT LOOKING AT THE DATES
SELECT SUM(new_cases) as total_cases,
SUM(cast(new_deaths as int)) as total_deaths,
SUM(cast(new_deaths as int))/SUM(New_cases) * 100 as death_percentage
FROM CovidDeaths
WHERE continent is not null
--GROUP BY date
ORDER BY 1,2
-- Lets look at the Vaccinations Table(An Inner Join was performed)
SELECT dea.continent,
dea.location,
dea.date,
dea.population,
vac.new_vaccinations
FROM CovidDeaths dea
JOIN CovidVaccinations vac
ON dea.location = vac.location AND dea.date = vac.date
WHERE dea.continent is not null
ORDER BY 1,2
-- LETS LOOK AT THE TOTAL VACCINATIONS FOR EACH COUNTRY
SELECT dea.location,
SUM(cast(vac.new_vaccinations as int)) as total_vac
FROM CovidDeaths dea
JOIN CovidVaccinations vac
ON dea.location = vac.location AND dea.date = vac.date
WHERE dea.continent is not null
GROUP BY dea.location
ORDER BY dea.location
-- Lets look at the Total Population vs Vaccinations
SELECT dea.continent,
dea.location,
dea.date,
dea.population,
vac.new_vaccinations,
SUM(convert(bigint, vac.new_vaccinations)) OVER (Partition by dea.Location ORDER BY
dea.location, dea.date) as rolling_vaccinated
FROM CovidDeaths dea
JOIN CovidVaccinations vac
ON dea.location = vac.location AND dea.date = vac.date
WHERE dea.continent is not null
ORDER BY 1,2
-- Lets use a CTE to perform Calculation on Partition By in previous query
WITH POP_VAC as
(SELECT dea.continent,
dea.location,
dea.date,
dea.population,
vac.new_vaccinations,
SUM(convert(bigint, vac.new_vaccinations)) OVER (Partition by dea.Location ORDER BY
dea.location, dea.date) as rolling_vaccinated
FROM CovidDeaths dea
JOIN CovidVaccinations vac
ON dea.location = vac.location AND dea.date = vac.date
WHERE dea.continent is not null
--ORDER BY 1,2
)
SELECT continent,
location,
date,
population,
new_vaccinations,
rolling_vaccinated,
(rolling_vaccinated/population) *100
FROM POP_VAC
-- Lets use a Temp Table to perform Calculation On Partition By in previous query
DROP TABLE IF EXISTS #Percent_population_vaccinated
CREATE TABLE #Percent_population_vaccinated(
continent nvarchar(255),
location nvarchar(255),
date datetime,
population numeric,
new_vaccinations numeric,
rolling_vaccinated numeric
)
INSERT INTO #Percent_population_vaccinated
SELECT dea.continent,
dea.location,
dea.date,
dea.population,
vac.new_vaccinations,
SUM(convert(bigint, vac.new_vaccinations)) OVER (Partition by dea.Location ORDER BY
dea.location, dea.date) as rolling_vaccinated
FROM CovidDeaths dea
JOIN CovidVaccinations vac
ON dea.location = vac.location AND dea.date = vac.date
WHERE dea.continent is not null
ORDER BY 2,3
SELECT continent,
location,
date,
population,
new_vaccinations,
rolling_vaccinated,
(rolling_vaccinated/population) *100
FROM #Percent_population_vaccinated
--Creating views to store data for later visualizations
CREATE VIEW Percent_population_vaccinated as
SELECT dea.continent,
dea.location,
dea.date,
dea.population,
vac.new_vaccinations,
SUM(convert(int, vac.new_vaccinations)) OVER (Partition by dea.Location ORDER BY
dea.location, dea.date) as rolling_vaccinated
FROM CovidDeaths dea
JOIN CovidVaccinations vac
ON dea.location = vac.location AND dea.date = vac.date
WHERE dea.continent is not null
--ORDER BY 2,3
CREATE VIEW Percentage_Death as
SELECT location,
date,
total_cases,
total_deaths,
(total_deaths/total_cases)* 100 as death_percentage
FROM CovidDeaths
WHERE location in ('Nigeria') and continent is not null
--ORDER BY 1,2
CREATE VIEW Percentage_Per_Population as
SELECT location,
date,
total_cases,
population,
(total_cases/population)*100 as covid_percentage_per_population
FROM CovidDeaths
WHERE location in ('Nigeria')
--ORDER BY total_cases
CREATE VIEW Percentage_Infection_Rate as
SELECT location,
population,
MAX(total_cases) as highest_infection_count,
MAX((total_cases/population))* 100 as percentage_infected_population
FROM CovidDeaths
WHERE continent is not null
GROUP BY location, population
--ORDER BY percentage_infected_population desc
CREATE VIEW Percentage_Death_Rate as
SELECT location,
population,
MAX(cast(total_deaths as int)) as highest_death_rate,
MAX((total_deaths/population)) *100 as percentage_death_population
FROM CovidDeaths
--WHERE continent is not null
GROUP BY location, population
--ORDER BY highest_death_rate desc
-- Querying a view
SELECT *
FROM Percent_population_vaccinated