-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL_code_1
185 lines (142 loc) · 6.96 KB
/
SQL_code_1
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
SELECT *
FROM `generated-case-336305.Covid19.covid_deaths`
WHERE continent IS NOT NULL
ORDER BY 3,4
SELECT *
FROM `generated-case-336305.Covid19.covid_vaccine`
WHERE continent IS NOT NULL
ORDER BY 3,4
--Selecting the data that we are going to be using
SELECT location, date, total_cases, new_cases, total_deaths, population
FROM `generated-case-336305.Covid19.covid_deaths`
WHERE continent IS NOT NULL
ORDER BY location, date
-- Looking at Total Cases vs Total Deaths in India
-- Likelyhood of dying if you contracted COVID in India
SELECT location, date, total_cases, total_deaths, (total_deaths/total_cases)*100 AS death_percent
FROM `generated-case-336305.Covid19.covid_deaths`
WHERE location = 'India' AND continent IS NOT NULL
ORDER BY location, date
-- Looking at Total Cases vs Total Deaths
SELECT location, date, total_cases, total_deaths, (total_deaths/total_cases)*100 AS death_percent
FROM `generated-case-336305.Covid19.covid_deaths`
WHERE continent IS NOT NULL
ORDER BY location, date
--Looking at Total Cases vs Population in India
-- Shows what percentage of population got Covid in India
SELECT location, date, total_cases, population, (total_cases/population)*100 AS infected_population_percent
FROM `generated-case-336305.Covid19.covid_deaths`
WHERE location = 'India' AND continent IS NOT NULL
ORDER BY location, date
-- Global Infected Population
SELECT location, date, total_cases, population, (total_cases/population)*100 AS infected_population_percent
FROM `generated-case-336305.Covid19.covid_deaths`
WHERE continent IS NOT NULL
ORDER BY location, date
-- Looking at countries with highest infection rates compared to poulation
SELECT location, MAX(total_cases) AS max_infection, population, MAX((total_cases/population))*100 AS max_infected_population_percent
FROM `generated-case-336305.Covid19.covid_deaths`
--WHERE location = 'India'
WHERE continent IS NOT NULL
GROUP BY location, population
ORDER BY max_infected_population_percent DESC
-- Showing countries with highest death counts per population
SELECT location, MAX(CAST(total_deaths AS int64)) AS max_death_count -- we casted the total_death as there was a problem with the data type
FROM `generated-case-336305.Covid19.covid_deaths`
WHERE continent IS NOT NULL
GROUP BY location
ORDER BY max_death_count DESC
-- Showing continents with highest death counts
SELECT continent, MAX(CAST(total_deaths AS int64)) AS max_death_count
FROM `generated-case-336305.Covid19.covid_deaths`
WHERE continent IS NOT NULL
GROUP BY continent
ORDER BY max_death_count DESC
-- Global Numbers
SELECT date, SUM(new_cases) AS total_newcases, SUM(CAST(new_deaths AS int64)) AS total_newdeaths, SUM(CAST(new_deaths AS int64))/SUM(new_cases)*100 AS global_death_percent
FROM `generated-case-336305.Covid19.covid_deaths`
WHERE continent IS NOT NULL
GROUP BY date
ORDER BY 1,2
-- without date
SELECT SUM(new_cases) AS total_newcases, SUM(CAST(new_deaths AS int64)) AS total_newdeaths, SUM(CAST(new_deaths AS int64))/SUM(new_cases)*100 AS global_death_percent
FROM `generated-case-336305.Covid19.covid_deaths`
WHERE continent IS NOT NULL
ORDER BY 1,2
-- Using JOINS
SELECT*
FROM `generated-case-336305.Covid19.covid_deaths` AS dea
JOIN `generated-case-336305.Covid19.covid_vaccine` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
-- Looking at Total Population vs Vaccination
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
FROM `generated-case-336305.Covid19.covid_deaths` AS dea
JOIN `generated-case-336305.Covid19.covid_vaccine` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
ORDER BY location, date
-- Breaking up by location using PARTITION BY and using ROLLING COUNT
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(CAST(vac.new_vaccinations AS int64)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS rolling_count_vac
FROM`generated-case-336305.Covid19.covid_deaths` AS dea
JOIN `generated-case-336305.Covid19.covid_vaccine` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
ORDER BY location, date
--New vaccine percentage
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(CAST(vac.new_vaccinations AS int64)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS rolling_count_vac, ( SUM(CAST(vac.new_vaccinations AS int64)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date)/population)*100 AS total_vac_percent
FROM `generated-case-336305.Covid19.covid_deaths` AS dea
JOIN `generated-case-336305.Covid19.covid_vaccine` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
ORDER BY location, date
-- Using CTE (Common Table Expression)
WITH pop_vs_vac (SELECT continent, location, date, population, new_vaccinations, rolling_count_vac)
AS
(
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(CAST(vac.new_vaccinations AS int64)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS rolling_count_vac
FROM `generated-case-336305.Covid19.covid_deaths` AS dea
JOIN `generated-case-336305.Covid19.covid_vaccine` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
--ORDER BY location, date
)
SELECT*, (rolling_count_vac/population)*100
FROM pop_vs_vac
-- Temp Table
DROP TABLE IF EXISTS
CREATE TABLE 'percent_population_vaccinated'
(
continent varchar(255),
location varchar(255),
date datetime,
population int64,
new_vaccinations int64,
rolling_count_vaccinated int64
)
INSERT INTO
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(CAST(vac.new_vaccinations AS int64)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS rolling_count_vac,
FROM `generated-case-336305.Covid19.covid_deaths` AS dea
JOIN `generated-case-336305.Covid19.covid_vaccine` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
-- ORDER BY location, date
SELECT*, (rolling_count_vac/population)*100
FROM percent_population_vaccinated
--Alternative to Temp Table
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(CAST(vac.new_vaccinations AS int64)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS rolling_count_vac,
FROM `generated-case-336305.Covid19.covid_deaths` AS dea
JOIN `generated-case-336305.Covid19.covid_vaccine` AS vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
--save the above table and reupload it, then run the following query:
SELECT continent, location, date, population, new_vaccinations, rolling_count_vac, (rolling_count_vac/population)*100 AS rolling_vac_percent,
FROM `generated-case-336305.Covid19.percent_population_vaccinated`
WHERE continent IS NOT NULL
ORDER BY date