-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCovid-19DataExploration.sql
215 lines (171 loc) · 7.24 KB
/
Covid-19DataExploration.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
--SELECT *
--FROM CovidDeaths
--ORDER BY 3,4
--SELECT *
--FROM CovidVaccinations
--ORDER BY 3,4
SELECT location, date, total_cases, new_cases, total_deaths, population
FROM CovidDeaths
ORDER BY 1,2
--looking at total cases vs total deaths
--shows the likelihood of dying if you contract Covid in your country
SELECT location, date, total_cases, total_deaths, (total_deaths/total_cases)*100 AS PercentageDeath
FROM CovidDeaths
WHERE location LIKE '%states%'
ORDER BY 1,2
--looking at the total cases vs population
--shows what percentage of population got Covid
SELECT location, date, population, total_cases, (total_cases/population)*100 AS PercentageCases
FROM CovidDeaths
--WHERE LOCATION LIKE '%canada%'
ORDER BY 1,2
--looking at countries with the highest infection rates compared to population
SELECT location, population, MAX(total_cases) AS HighestInfectionCount, MAX(total_cases/population)*100 AS HighestInfectionRate
FROM CovidDeaths
--WHERE LOCATION LIKE '%canada%'
GROUP BY continent, population
ORDER BY HighestInfectionRate DESC
--showing countires with the highest death count per population
SELECT location, MAX(CAST(total_deaths as INT)) AS TotalDeathCount
FROM CovidDeaths
--WHERE LOCATION LIKE '%canada%'
WHERE Continent IS NOT NULL
GROUP BY continent
ORDER BY TotalDeathCount DESC
--Lets break things down by continent
SELECT location, MAX(CAST(total_deaths as INT)) AS TotalDeathCount
FROM CovidDeaths
--WHERE LOCATION LIKE '%canada%'
WHERE Continent IS NULL
GROUP BY continent
ORDER BY TotalDeathCount DESC
--showing continents with the highest death count per population
SELECT continent, MAX(CAST(total_deaths as INT)) AS TotalDeathCount
FROM CovidDeaths
--WHERE LOCATION LIKE '%canada%'
WHERE Continent IS NOT NULL
GROUP BY continent
ORDER BY TotalDeathCount DESC
--Global numbers
--sum of new_cases per day across the world
SELECT date, SUM(new_cases) AS DailyNewCases
FROM CovidDeaths
--WHERE location LIKE '%canada%'
WHERE continent IS NOT NULL
GROUP BY date
ORDER BY 1,2
SELECT date, SUM(new_cases) AS DailyNewCases, SUM(CAST(new_deaths AS INT)) AS DailyNewDeaths
FROM CovidDeaths
--WHERE location LIKE '%canada%'
WHERE continent IS NOT NULL
GROUP BY date
ORDER BY 1,2
--Daily new death percentage across the world
SELECT date, SUM(new_cases) AS DailyNewCases, SUM(CAST(new_deaths AS INT)) AS DailyNewDeaths, SUM(CAST(new_deaths AS INT))/SUM(new_cases) AS DailyNewDeathPercentage
FROM CovidDeaths
--WHERE location LIKE '%canada%'
WHERE continent IS NOT NULL
GROUP BY date
ORDER BY 1,2
--Overall Daily New's Across the World
SELECT SUM(new_cases) AS DailyNewCases, SUM(CAST(new_deaths AS INT)) AS DailyNewDeaths, SUM(CAST(new_deaths AS INT))/SUM(new_cases) AS DailyNewDeathPercentage
FROM CovidDeaths
--WHERE location LIKE '%canada%'
WHERE continent IS NOT NULL
ORDER BY 1,2
SELECT date, SUM(new_cases) AS DailyNewCases, SUM(CAST(new_deaths AS INT)) AS DailyNewDeaths, SUM(CAST(new_deaths AS INT))/SUM(new_cases) AS DailyNewDeathPercentage
FROM CovidDeaths
--WHERE location LIKE '%canada%'
WHERE continent IS NOT NULL
GROUP BY date
ORDER BY 1,2
--looking at total population vs vaccinations
--JOINS
SELECT *
FROM CovidDeaths
JOIN CovidVaccinations
ON CovidDeaths.location = CovidVaccinations.location
AND CovidDeaths.date = CovidVaccinations.date
--Total population vs New vaccinations
SELECT CovidDeaths.continent, CovidDeaths.location, CovidDeaths.date, CovidDeaths.population, CovidVaccinations.new_vaccinations
FROM CovidDeaths
JOIN CovidVaccinations
ON CovidDeaths.location = CovidVaccinations.location
AND CovidDeaths.date = CovidVaccinations.date
WHERE CovidDeaths.continent IS NOT NULL
ORDER BY 1,2,3
--Daily Vaccinations
SELECT CovidDeaths.continent, CovidDeaths.location, CovidDeaths.date, CovidDeaths.population, CovidVaccinations.new_vaccinations,
SUM(CAST(CovidVaccinations.new_vaccinations AS INT)) OVER(PARTITION BY CovidDeaths.location ORDER BY CovidDeaths.location, CovidDeaths.date) AS DailyVaccinations
FROM CovidDeaths
JOIN CovidVaccinations
ON CovidDeaths.location = CovidVaccinations.location
AND CovidDeaths.date = CovidVaccinations.date
WHERE CovidDeaths.continent IS NOT NULL
ORDER BY 1,2,3
--Number of people vaccinated in a country
--USE CTE
--With PopVacc (continent, location, date, population, new_vaccinations, DailyVaccinations)
--AS
--(
--SELECT CovidDeaths.continent, CovidDeaths.location, CovidDeaths.date, CovidDeaths.population, CovidVaccinations.new_vaccinations,
--SUM(CAST(CovidVaccinations.new_vaccinations AS INT)) OVER(PARTITION BY CovidDeaths.location ORDER BY CovidDeaths.location, CovidDeaths.date) AS DailyVaccinations
--FROM CovidDeaths
--JOIN CovidVaccinations
-- ON CovidDeaths.location = CovidVaccinations.location
-- AND CovidDeaths.date = CovidVaccinations.date
-- WHERE CovidDeaths.continent IS NOT NULL
--)
--SELECT *
--FROM PopVacc
--Percentage of the total population vaccinated
With PopVacc (continent, location, date, population, new_vaccinations, DailyVaccinations)
AS
(
SELECT CovidDeaths.continent, CovidDeaths.location, CovidDeaths.date, CovidDeaths.population, CovidVaccinations.new_vaccinations,
SUM(CAST(CovidVaccinations.new_vaccinations AS INT)) OVER(PARTITION BY CovidDeaths.location ORDER BY CovidDeaths.location, CovidDeaths.date) AS DailyVaccinations
FROM CovidDeaths
JOIN CovidVaccinations
ON CovidDeaths.location = CovidVaccinations.location
AND CovidDeaths.date = CovidVaccinations.date
WHERE CovidDeaths.continent IS NOT NULL
)
SELECT *, (DailyVaccinations/population)*100
FROM PopVacc
--TEMP TABLE
Drop table if exists #PercentagePopulationVaccinated
Create Table #PercentagePopulationVaccinated
(
Continent nvarchar(255),
Location nvarchar(255),
Date datetime,
Population numeric,
new_vaccinations numeric,
DailyVaccinations numeric
)
INSERT INTO #PercentagePopulationVaccinated
SELECT CovidDeaths.continent, CovidDeaths.location, CovidDeaths.date, CovidDeaths.population, CovidVaccinations.new_vaccinations,
SUM(CAST(CovidVaccinations.new_vaccinations AS INT)) OVER(PARTITION BY CovidDeaths.location ORDER BY CovidDeaths.location, CovidDeaths.date) AS DailyVaccinations
FROM CovidDeaths
JOIN CovidVaccinations
ON CovidDeaths.location = CovidVaccinations.location
AND CovidDeaths.date = CovidVaccinations.date
WHERE CovidDeaths.continent IS NOT NULL
SELECT *, (DailyVaccinations/population)*100
FROM #PercentagePopulationVaccinated
--Creating view to store data for later visualization
Create view TotalDeathCountPerContinent as
SELECT continent, MAX(CAST(total_deaths as INT)) AS TotalDeathCount
FROM CovidDeaths
WHERE Continent IS NOT NULL
GROUP BY continent
Create view PercentagePopulationVaccinated AS
SELECT CovidDeaths.continent, CovidDeaths.location, CovidDeaths.date, CovidDeaths.population, CovidVaccinations.new_vaccinations,
SUM(CAST(CovidVaccinations.new_vaccinations AS INT)) OVER(PARTITION BY CovidDeaths.location ORDER BY CovidDeaths.location, CovidDeaths.date) AS DailyVaccinations
FROM CovidDeaths
JOIN CovidVaccinations
ON CovidDeaths.location = CovidVaccinations.location
AND CovidDeaths.date = CovidVaccinations.date
WHERE CovidDeaths.continent IS NOT NULL
SELECT *
FROM PercentagePopulationVaccinated