-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCovid Project.sql
More file actions
338 lines (290 loc) · 7.49 KB
/
Covid Project.sql
File metadata and controls
338 lines (290 loc) · 7.49 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
Covid 19 Data Exploration
Skills used: Joins, CTE's, Temp Tables, Windows Functions, Aggregate Functions, Creating Views, Converting Data Types
*/
-- Select Data that we are going to be starting with
SELECT
location,
date,
total_cases,
new_cases,
total_deaths,
population
FROM CovidDeaths
ORDER BY 1,2
--Some of the locations are group where we dont have continent so we exclude that
-- Total Cases vs Total Deaths
SELECT
location,
date,
total_cases ,
total_deaths ,
population,
(total_deaths/total_cases)*100 AS Death_Rate_Percentage
FROM CovidDeaths
WHERE continent is not null
Order BY 1,2
-- Shows likelihood of dying if you contract covid in your country
SELECT
location,
date,
total_cases ,
total_deaths ,
population,
(total_deaths/total_cases)*100 AS DeathPercentage
FROM CovidDeaths
WHERE location = 'Luxembourg' AND continent is not null
Order BY 1,2
--Show what percentage of population got Covid
SELECT
location,
date,
total_cases ,
total_deaths ,
population,
(total_cases/population)*100 AS InfectedPeoplePercentage
FROM CovidDeaths
WHERE continent is not null
Order BY 1,2
--Analysing for Luxembourg
SELECT
location,
date,
total_cases ,
total_deaths ,
population,
(total_cases/population)*100 AS InfectedPeoplePercentage
FROM CovidDeaths
WHERE location = 'Luxembourg' AND continent is not null
Order BY (total_cases/population)*100 desc
--Looking at countries with highset cases
SELECT
location,
date,
total_cases ,
total_deaths ,
population
FROM CovidDeaths
WHERE continent is not null
Order BY total_cases DESC
--Looking at countries with highset Infection rates compared to population
SELECT
location,
MAX(total_cases) AS HigestInfectionCount,
population,
MAX((total_cases/population)*100) AS MaxInfectedPeoplePercentage
FROM CovidDeaths
WHERE continent is not null
GROUP BY location,population
Order BY MaxInfectedPeoplePercentage DESC
--Looking at Luxembourg
SELECT
location,
MAX(total_cases) AS HigestInfectionCount,
population,
MAX((total_cases/population)*100) AS MaxInfectedPeoplePercentage
FROM CovidDeaths
WHERE location = 'Luxembourg' AND continent is not null
GROUP BY location,population
Order BY MaxInfectedPeoplePercentage DESC
--10percent Population was infected oh lala including me ;)
--Showing the countries with highest death count compared to population
SELECT
location,
MAX(total_deaths) AS TotalDeathCount,
population
FROM CovidDeaths
WHERE continent is not null
GROUP BY location,population
Order BY TotalDeathCount DESC
--LETS EXPLORE NOW BASED ON CONTINENT
SELECT
continent,
MAX(total_deaths) AS TotalDeathCount
FROM CovidDeaths
WHERE continent is not null
GROUP BY continent
Order BY TotalDeathCount DESC
--this query is not giving us the actual numbers as some of the continents are null and then in location it is grouped by continent
--Lets include location in above query
SELECT
location,
MAX(total_deaths) AS TotalDeathCount
FROM CovidDeaths
WHERE continent is null
GROUP BY location
Order BY TotalDeathCount DESC
--Grouping things by continents
with Continent AS
(
SELECT
continent,
location,
MAX(total_cases) AS HigestInfectionCount,
population,
MAX((total_cases/population)*100) AS MaxInfectedPeoplePercentage
FROM CovidDeaths
WHERE continent is not null
GROUP BY continent,location,population
),
RankedContinent AS
(
SELECT
continent,
location,
MaxInfectedPeoplePercentage,
ROW_NUMBER() OVER (PARTITION BY continent ORDER BY MaxInfectedPeoplePercentage DESC) AS rn
FROM Continent
)
SELECT continent,
location,
MaxInfectedPeoplePercentage
FROM RankedContinent
WHERE rn = 1
ORDER BY MaxInfectedPeoplePercentage DESC
--We can see highest number of cases are recorded in Europe then Asia
--Lets Group data by date
SELECT
date,location,
MAX(total_deaths) AS TotalDeathCount
FROM CovidDeaths
WHERE continent is not null
GROUP BY date,location
Order BY TotalDeathCount DESC
--Lets see how many new cases were recorded and how it was suming up across the world
SELECT
date,
SUM(new_cases) AS SumOfNewCases,
SUM(new_deaths) AS SumOfNewDeaths,
(SUM(new_deaths)/SUM(new_cases))*100 AS DeathPercentage
FROM CovidDeaths
WHERE continent is not null
GROUP BY date
Order BY 1,2
--We can see it started towards the end of janurary 2020
--Lets see the total across the world
SELECT
SUM(new_cases) AS SumOfNewCases,
SUM(new_deaths) AS SumOfNewDeaths,
(SUM(new_deaths)/SUM(new_cases))*100 AS DeathPercentage
FROM CovidDeaths
WHERE continent is not null
--GROUP BY date
Order BY 1,2
--Lets join our two table
SELECT *
FROM CovidDeaths cd
JOIN CovidVaccinations cv
ON cd.iso_code = cv.iso_code
--Lets see Total Population vs new vaccinations
SELECT
cd.continent,
cd.location,
cd.date,
population,
cv.new_vaccinations
FROM CovidDeaths cd
JOIN CovidVaccinations cv
ON cd.location = cv.location
WHERE cv.continent is NOT NULL
ORDER BY 2,3
--Lets see now Total vaccinations Vs. Population
SELECT
cd.continent,
cd.location,
cd.date, -- we are looking here on daily basis
population,
cv.total_vaccinations
FROM CovidDeaths cd
JOIN CovidVaccinations cv
ON cd.location = cv.location
WHERE cv.continent is NOT NULL
ORDER BY 2,3
--Looking at Rolling total of new vaccinations now
SELECT
cd.continent,
cd.location,
cd.date,
population,
cv.new_vaccinations,
SUM(cv.new_vaccinations) OVER (Partition by cd.location ORDER BY cd.location,cd.date) AS RollingTotal
FROM CovidDeaths cd
JOIN CovidVaccinations cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cv.continent is NOT NULL
ORDER BY 2,3
--USE CTE
WITH PopVsVac AS
(
SELECT
cd.continent,
cd.location,
cd.date,
population,
cv.new_vaccinations,
SUM(cv.new_vaccinations) OVER (Partition by cd.location ORDER BY cd.location,cd.date) AS RollingTotal
FROM CovidDeaths cd
JOIN CovidVaccinations cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cv.continent is NOT NULL
--ORDER BY 2,3
)
SELECT *,(RollingTotal/population)*100
FROM PopVsVac;
--TEMP TABLE
CREATE TABLE #PercentPopulationVaccinated
(
continent nvarchar (255),
location nvarchar(255),
date datetime,
population numeric,
new_vaccinations numeric,
rollingtotal numeric
)
INSERT INTO #PercentPopulationVaccinated
SELECT
cd.continent,
cd.location,
cd.date,
population,
cv.new_vaccinations,
SUM(cv.new_vaccinations) OVER (Partition by cd.location ORDER BY cd.location,cd.date) AS RollingTotal
FROM CovidDeaths cd
JOIN CovidVaccinations cv
ON cd.location = cv.location
AND cd.date = cv.date
WHERE cv.continent is NOT NULL
ORDER BY 2,3
SELECT continent,MAX(rollingtotal) AS MaxRollingTotal_NewVaccinations
FROM #PercentPopulationVaccinated
GROUP BY continent
--CREATING VIEWS
CREATE VIEW PopulationsVsVaccinations AS
SELECT
cd.continent,
cd.location,
cd.date, -- we are looking here on daily basis
population,
cv.total_vaccinations
FROM CovidDeaths cd
JOIN CovidVaccinations cv
ON cd.location = cv.location
WHERE cv.continent is NOT NULL
--ORDER BY 2,3
CREATE VIEW DeathsVsCasesVsVaccines AS
SELECT
cd.continent,
cd.location,
cd.date,
cd.total_cases,
cd.total_deaths,
population,
cv.new_vaccinations,
cv.total_vaccinations
FROM CovidDeaths cd
JOIN CovidVaccinations cv
ON cd.location = cv.location
WHERE cv.continent is NOT NULL
--ORDER BY 2,3