-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCovid19_data_exploration.sql
174 lines (159 loc) · 4.17 KB
/
Covid19_data_exploration.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
/*
Covid 19 Data Exploration
SQL skills demonstrated: Joins, CTEs, Temporary Tables, Window Functions, Aggregate Functions, Creating Views
This script analyzes Covid-19 data to explore different aspects like infection rates, vaccination coverage, and mortality rates across different regions and dates.
*/
-- Set the working database
USE PortfolioProject1;
-- Basic Data Exploration: Listing Covid Death Details
SELECT
*
FROM
CovidDeaths
WHERE
continent != ''
ORDER BY 3, 4;
-- Detailed Case Report by Location and Date
SELECT
Location,
date,
total_cases,
new_cases,
total_deaths,
population
FROM
CovidDeaths
WHERE
continent != ''
ORDER BY 1, 2;
-- Calculate Death Percentage: Likelihood of Dying from Covid if Contracted
DROP VIEW IF EXISTS DeathPercentage;
CREATE VIEW DeathPercentage AS
SELECT
Location,
date,
total_cases,
total_deaths,
(total_deaths / total_cases) * 100 AS DeathPercentage
FROM
CovidDeaths
WHERE
continent != ''
ORDER BY 1, 2;
-- Calculate Case Percentage: Percentage of Population that Contracted Covid
DROP VIEW IF EXISTS CasePercentage;
CREATE VIEW CasePercentage AS
SELECT
Location,
date,
population,
total_cases,
(total_cases / population) * 100 AS CasePercentage
FROM
CovidDeaths
WHERE
continent != ''
ORDER BY 1, 2;
-- Infection Rate Analysis: Identifying Countries with the Highest Infection Rates
DROP VIEW IF EXISTS HighestInfectionRate;
CREATE VIEW HighestInfectionRate AS
SELECT
Location,
population,
MAX(total_cases) AS HighestInfectionCount,
MAX((total_cases / population)) * 100 AS PercentPopulationInfected
FROM
CovidDeaths
WHERE
continent != ''
GROUP BY location, population
ORDER BY PercentPopulationInfected DESC;
-- Mortality Analysis: Countries with the Highest Covid Death Counts
DROP VIEW IF EXISTS HighestDeathCount;
CREATE VIEW HighestDeathCount AS
SELECT
Location, MAX(total_deaths) AS TotalDeathCount
FROM
CovidDeaths
WHERE
continent != ''
GROUP BY location
ORDER BY TotalDeathCount DESC;
-- Continental Analysis: Continents with the Highest Death Counts
DROP VIEW IF EXISTS ContinentalDeathCount;
CREATE VIEW ContinentalDeathCount AS
SELECT
continent, MAX(total_deaths) AS TotalDeathCount
FROM
CovidDeaths
WHERE
continent != ''
GROUP BY continent
ORDER BY TotalDeathCount DESC;
-- Global Analysis: Daily New Cases and Death Percentages
DROP VIEW IF EXISTS GlobalNumbers;
CREATE VIEW GlobalNumbers AS
SELECT
date,
SUM(new_cases) AS TotalCases,
SUM(new_deaths) AS NewDeaths,
SUM(new_deaths) / SUM(new_cases) * 100 AS DeathPercentage
FROM
CovidDeaths
WHERE
continent != ''
GROUP BY date
ORDER BY 1, 2;
-- Vaccination Coverage Analysis: Rolling Vaccination Numbers and Percentages
DROP VIEW IF EXISTS VaccinationCoverage;
CREATE VIEW VaccinationCoverage AS
SELECT
dea.continent,
dea.location,
dea.date,
dea.population,
vac.new_vaccinations,
SUM(vac.new_vaccinations) OVER (PARTITION BY dea.location ORDER BY dea.date) AS RollingPeopleVaccinated,
(RollingPeopleVaccinated / population) * 100 AS VaccinationPercentage
FROM
CovidDeaths AS dea
JOIN
CovidVaccinations AS vac
ON
dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent != ''
ORDER BY 2, 3;
-- Temporary Table for Quick Data Retrieval: Vaccination vs Population
DROP TABLE IF EXISTS TempPercentPopulationVaccinated;
CREATE TEMPORARY TABLE IF NOT EXISTS TempPercentPopulationVaccinated
(
continent VARCHAR(100),
location VARCHAR(100),
date DATE,
population BIGINT,
new_vaccinations INT,
RollingPeopleVaccinated BIGINT
);
INSERT INTO TempPercentPopulationVaccinated
SELECT
dea.continent,
dea.location,
dea.date,
dea.population,
vac.new_vaccinations,
SUM(vac.new_vaccinations) OVER (Partition by dea.location ORDER BY dea.date)
AS RollingPeopleVaccinated
FROM
CovidDeaths AS dea
JOIN
CovidVaccinations AS vac
ON
dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent != '';
SELECT
*,
(RollingPeopleVaccinated / Population) * 100 AS RollingPeoplePercentage
FROM
TempPercentPopulationVaccinated;