-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCovid19_Analysis.sql
262 lines (207 loc) · 8.39 KB
/
Covid19_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
/*
COVID-19 Data Exploration
Source : https://ourworldindata.org/covid-deaths
*/
-- Table 1 (CovidDeaths : Data Imported to SQL Developer)
SELECT COUNT(*) FROM COVIDDEATHS
WHERE CONTINENT IS NOT NULL;
SELECT * FROM COVIDDEATHS
WHERE CONTINENT IS NOT NULL
ORDER BY 3,4;
-- Table 2 (Vaccinations: Data Imported to SQL Developer)
SELECT * FROM COVIDVACCINATIONS
WHERE CONTINENT IS NOT NULL
ORDER BY 3,4;
-- Selecting COVID data from Table 1(CovidDeaths).
SELECT
Location
,Date_report
,total_cases
,new_cases
,total_deaths
,population
FROM COVIDDEATHS
WHERE CONTINENT IS NOT NULL
ORDER BY 1,2;
-- Lets look at Total Cases vs Total Deaths (United States)
SELECT
Location
,Date_report
,total_cases
,COALESCE(total_deaths,0) AS total_dealths -- Don't want to show NULL value
, COALESCE((total_deaths/total_cases)*100,0) AS DeathPercentage
FROM COVIDDEATHS
WHERE location LIKE '%States%'
AND CONTINENT IS NOT NULL
ORDER BY 1,2;
-- Lets look at Total Cases vs Population for USA
-- What percentage of population (USA) got Covid
SELECT
Location
,Date_report
,total_cases
,Population
, COALESCE((total_cases/Population)*100,0) AS CovidPercentage
FROM COVIDDEATHS
WHERE location LIKE '%States%'
AND CONTINENT IS NOT NULL
ORDER BY 1,2;
--- Countries with highest COVID-19 infection rate compared to population
SELECT
Location
,Population
,MAX(total_cases) as HighestInfectionCount
,MAX((total_cases/Population)*100) AS PercentPopulationInfected
FROM COVIDDEATHS
WHERE CONTINENT IS NOT NULL
GROUP BY Location,Population
ORDER BY PercentPopulationInfected DESC
NULLS LAST;
--- Countries with highest COVID-19 Dealth Count per population
SELECT
Location
,MAX(total_deaths) as HighestDeathCount
FROM COVIDDEATHS
WHERE CONTINENT IS NOT NULL
GROUP BY Location
ORDER BY HighestDeathCount DESC
NULLS LAST;
--- Showing Continents with highest COVID-19 Dealth Count per population
SELECT
Continent
,MAX(total_deaths) as HighestDeathCount
FROM COVIDDEATHS
WHERE CONTINENT IS NOT NULL
GROUP BY Continent
ORDER BY HighestDeathCount DESC ;
-- Checking global counts per date
Select
date_report
,SUM(new_cases) as total_cases
,SUM(new_deaths) as total_deaths
,SUM(new_deaths)/SUM(New_Cases)*100 as DeathPercentage
FROM
CovidDeaths
WHERE Continent IS NOT NULL
GROUP BY date_report
ORDER BY 1;
-- Lets check the total world data (removed GROUP BY clause)
Select
SUM(new_cases) as total_cases
,SUM(new_deaths) as total_deaths
,SUM(new_deaths)/SUM(New_Cases)*100 as DeathPercentage
FROM
CovidDeaths
WHERE Continent IS NOT NULL;
-- AND date_report < '30-APR-21'
--- Now, lets explore other table (CovidVaccinations)
-- Total Population vs Vaccinations
SELECT
death.continent
,death.location
,death.date_report
,death.population
,vac.new_vaccinations
, SUM(vac.new_vaccinations)
OVER (PARTITION BY death.location ORDER BY death.date_report) AS RollingPeopleVaccinated
FROM CovidDeaths death
Join CovidVaccinations vac
On death.date_report = vac.date_report
AND death.location = vac.location
WHERE death.continent IS NOT NULL
--- AND death.location = 'India'
ORDER BY 2,3;
-- Using CTE to perform calculation done in the previous query
WITH PopVsVacc AS
(
SELECT
death.continent
,death.location
,death.date_report
,death.population
,vac.new_vaccinations
, SUM(vac.new_vaccinations)
OVER (PARTITION BY death.location ORDER BY death.location,
death.date_report) AS RollingPeopleVaccinated
FROM CovidDeaths death
Join CovidVaccinations vac
On death.date_report = vac.date_report
AND death.location = vac.location
WHERE death.continent IS NOT NULL
--- AND death.location = 'India'
ORDER BY 2,3
)
SELECT
continent
,location
,date_report
,population
,new_vaccinations
,RollingPeopleVaccinated
,(RollingPeopleVaccinated/Population)*100 AS PercentPeopleVaccinated
FROM
PopVsVacc;
--- Temporary tables
CREATE TABLE PercentPopulationVaccinated
(
Continent VARCHAR(255)
,Location VARCHAR(255)
,Date_recorded DATE
,Population NUMBER
,New_Vaccinations NUMBER
,RollingPeopleVaccinated NUMBER
);
INSERT INTO PercentPopulationVaccinated
SELECT
death.continent
,death.location
,death.date_report
,death.population
,vac.new_vaccinations
, SUM(vac.new_vaccinations)
OVER (PARTITION BY death.location ORDER BY death.location,
death.date_report) AS RollingPeopleVaccinated
FROM CovidDeaths death
Join CovidVaccinations vac
On death.date_report = vac.date_report
AND death.location = vac.location
WHERE death.continent IS NOT NULL
;
-- Select records from Temp table
SELECT *
FROM PercentPopulationVaccinated;
--- Queries for Tableau dashboards.
--- 1
Select
SUM(new_cases) as total_cases
,SUM(new_deaths) as total_deaths
,SUM(new_deaths)/SUM(New_Cases)*100 as DeathPercentage
FROM
CovidDeaths
WHERE Continent IS NOT NULL;
--- 2
Select location, SUM(new_deaths) as TotalDeathCount
From CovidDeaths
Where continent is null
and location not LIKE '%income%' AND --- some weird location
location NOT IN ('World', 'European Union', 'International')
Group by location
order by TotalDeathCount desc;
-- 3
Select Location,
COALESCE(Population,0) Population,
COALESCE(MAX(total_cases),0) as HighestInfectionCount,
COALESCE(Max((total_cases/population))*100,0) as PercentPopulationInfected
From CovidDeaths
GROUP BY Location, Population
order by PercentPopulationInfected desc;
-- 4
Select Location
,COALESCE(Population,0) Population,
date_report,
COALESCE(MAX(total_cases),0) as HighestInfectionCount,
COALESCE(Max((total_cases/population))*100,0) as PercentPopulationInfected
From CovidDeaths
Group by Location, Population, date_report
order by PercentPopulationInfected desc;
--- 5