-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLQuery1.sql
More file actions
205 lines (175 loc) · 5.72 KB
/
SQLQuery1.sql
File metadata and controls
205 lines (175 loc) · 5.72 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
Select *
From Project..CovidDeaths
where continent is not NULL
Order by 3,4
Select *
From Project..CovidVaccinations
where continent is not NULL
Order by 3,4
-- select Data that we are going to be using
Select Location,date,total_cases,new_cases,total_deaths,population
From Project..covidDeaths
where continent is not NULL
order by 1,2
--Looking at Total cases vs Total Deaths
SELECT
Location,
Date,
Total_Cases,
New_Cases,
Total_Deaths,
(CAST(Total_Deaths AS float) / CAST(Total_Cases AS float)) * 100 AS DeathPercentage
FROM
Project..covidDeaths
WHERE Location LIKE '%states%'
ORDER BY
Location, Date;
-- Looking at Total Case Vs Population
-- Shows what percentage of population got Covid
SELECT
Location,
Date,
Total_Cases,
Population,
(CAST(Total_Cases AS float) / CAST(Population AS float)) * 100 AS DeathPercentage
FROM
Project..covidDeaths
--WHERE Location LIKE '%states%'
ORDER BY
Location, Date;
--Looking at Countries with Highest Infection Rate Compared to Population
SELECT
Location,
Population,
MAX(Total_Cases) AS HighestInfectionCount,
(MAX(Total_Cases) / Population) * 100 AS PercentPopulationInfected
FROM
Project..covidDeaths
--WHERE Location LIKE '%states%'
GROUP BY
Location, Population
ORDER BY
PercentPopulationInfected DESC
--Showing Countries with Highest Death Count per Population
SELECT
Location,
MAX(CAST(Total_deaths AS int)) AS TotalDeathCount
FROM
Project..covidDeaths
--WHERE Location LIKE '%states%'
where continent is not NULL
GROUP BY
Location
ORDER BY
TotalDeathCount DESC;
--LET'S BREAK THINGS DOWN BY CONTINENT
--showing continents with the highest death count for population
SELECT
continent,
MAX(CAST(Total_deaths AS int)) AS TotalDeathCount
FROM
Project..covidDeaths
--WHERE Location LIKE '%states%'
where continent is not NULL
GROUP BY
continent
ORDER BY
TotalDeathCount DESC;
--GLOBAL NUMBERS
SELECT
SUM(CAST(new_cases AS int)) AS Total_Cases,
SUM(CAST(new_deaths AS int)) AS Total_Deaths,
(SUM(CAST(new_deaths AS int)) / NULLIF(SUM(CAST(new_cases AS int)), 0)) * 100 AS DeathPercentage
FROM
Project..covidDeaths
--WHERE Location LIKE '%states%'
WHERE continent IS NOT NULL
GROUP BY
Date
ORDER BY 1,2
---Looking at Total Population VS Vaccinations
SELECT
dea.continent,
dea.location,
dea.date,
dea.population,
vac.new_vaccinations,
SUM(ISNULL(CONVERT(BIGINT, vac.new_vaccinations), 0)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS RollingPeopleVaccinated,
(SUM(ISNULL(CONVERT(BIGINT, vac.new_vaccinations), 0)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) / NULLIF(dea.population, 0)) * 100 AS VaccinationPercentage
FROM
Project..covidDeaths dea
JOIN
Project..CovidVaccinations vac ON dea.location = vac.location AND dea.date = vac.date
WHERE
dea.continent IS NOT NULL
ORDER BY
dea.location, dea.date;
-- USE CTE
WITH PopvsVac (Continent, Location, Date, Population, New_Vaccination, RollingPeopleVaccinated, VaccinationPercentage)
AS
(
SELECT
dea.continent,
dea.location,
dea.date,
dea.population,
vac.new_vaccinations,
SUM(ISNULL(CONVERT(BIGINT, vac.new_vaccinations), 0)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS RollingPeopleVaccinated,
(SUM(ISNULL(CONVERT(BIGINT, vac.new_vaccinations), 0)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) / NULLIF(dea.population, 0)) * 100 AS VaccinationPercentage
FROM
Project..covidDeaths dea
JOIN
Project..CovidVaccinations vac ON dea.location = vac.location AND dea.date = vac.date
WHERE
dea.continent IS NOT NULL
)
SELECT *,
FROM PopvsVac;
--TEMP TABLE
-- Drop table if it exists
IF OBJECT_ID('PercentPopulationVaccinated', 'U') IS NOT NULL
DROP TABLE PercentPopulationVaccinated;
-- Create table
CREATE TABLE PercentPopulationVaccinated (
Continent NVARCHAR(255),
Location NVARCHAR(255),
Date DATETIME,
Population NUMERIC,
New_vaccinations NUMERIC,
RollingPeopleVaccinated NUMERIC,
VaccinationPercentage NUMERIC
);
-- Insert data
INSERT INTO PercentPopulationVaccinated
SELECT
dea.continent,
dea.location,
dea.date,
dea.population,
vac.new_vaccinations,
SUM(ISNULL(CONVERT(BIGINT, vac.new_vaccinations), 0)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS RollingPeopleVaccinated,
(SUM(ISNULL(CONVERT(BIGINT, vac.new_vaccinations), 0)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) / NULLIF(dea.population, 0)) * 100 AS VaccinationPercentage
FROM
Project..covidDeaths dea
JOIN
Project..CovidVaccinations vac ON dea.location = vac.location AND dea.date = vac.date
WHERE
dea.continent IS NOT NULL;
-- Creating View to store data for later visualizations
CREATE VIEW PercentPopulationsVaccinated AS
SELECT
dea.continent,
dea.location,
dea.date,
dea.population,
vac.new_vaccinations,
SUM(ISNULL(CONVERT(BIGINT, vac.new_vaccinations), 0)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS RollingPeopleVaccinated,
(SUM(ISNULL(CONVERT(BIGINT, vac.new_vaccinations), 0)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) / NULLIF(dea.population, 0)) * 100 AS VaccinationPercentage
FROM
Project..covidDeaths dea
JOIN
Project..CovidVaccinations vac ON dea.location = vac.location AND dea.date = vac.date
WHERE
dea.continent IS NOT NULL;
Select *
From PercentPopulationsVaccinated