-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCovidSQLExploration.sql
173 lines (150 loc) · 6.25 KB
/
CovidSQLExploration.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
--SELECT *
--FROM coviddeaths
--order by 3,4
--SELECT *
--FROM covidvaccinations
--order by 3,4
--Select Data that I am going to be using
Select Location, date, total_cases, new_cases, total_deaths, population
From coviddeaths
order by 1,2
--Looking at Total Cases vs Total Deaths
Select Location, date, total_cases, total_deaths, (total_deaths/total_cases)*100 AS DeathPercentage
From coviddeaths
order by 1,2
--Looking at Total Cases vs Population
Select Location, date, total_cases, Population, (total_cases/population)*100 AS CovidPercentage
From coviddeaths
--Where Location like '%states%'
order by 1,2
--Looking at what countries have the highest infection rates per population size
Select Location, MAX(total_cases) as HighestInfectionCount, MAX(total_cases/population)*100 AS InfectionPercentage
From coviddeaths
Group by Population, Location
order by InfectionPercentage
--Looking at countries wih Highest Death Count per Population
Select Location, MAX(cast(total_deaths as int)) as TotalDeathCount
From coviddeaths
Group by Location
Order by TotalDeathCount DESC
--Looking at infection percentage vs death percentage
Select Location, MAX(total_cases) as HighestInfectionCount, MAX(total_cases/population)*100 AS InfectionPercentage, MAX(total_deaths/total_cases)*100 AS DeathsPercentage
From coviddeaths
Group by Population, Location
Having Location like 'Norway'
order by DeathsPercentage
--Looking at cases percentage vs vaccination percentage per population
Select deaths.Location, MAX(deaths.total_cases/deaths.population)*100 AS InfectionPercentage, MAX(vac.people_vaccinated/deaths.population)*100 AS VaccPercentage
From coviddeaths deaths
Join covidvaccinations vac
On deaths.location = vac.location
Group by Population, deaths.location
Having (deaths.location like 'Canada'
or deaths.location like '%states%'
or deaths.location like 'Sweden'
or deaths.location like 'Denmark'
or deaths.location like 'Switzerland'
or deaths.location like 'Norway')
Order by InfectionPercentage DESC
--Looking at death percentage vs vaccination percentage per population
Select deaths.Location, MAX(deaths.total_deaths/deaths.population)*100 AS DeathsPercentage, MAX(vac.people_vaccinated/deaths.population)*100 AS VaccPercentage
From coviddeaths deaths
Join covidvaccinations vac
On deaths.location = vac.location
Group by Population, deaths.location
Order by DeathsPercentage DESC
--death percentage vs vaccination percentage vs infection percentage
Select deaths.Location, MAX(deaths.total_cases/deaths.population)*100 AS InfectionPercentage, MAX(vac.people_vaccinated/deaths.population)*100 AS VaccPercentage, MAX(cast(deaths.total_deaths as int)/deaths.total_cases)*100 AS TotalDeathCount
From coviddeaths deaths
Join covidvaccinations vac
On deaths.location = vac.location
Group by Population, deaths.location
Having (deaths.location like 'Canada'
or deaths.location like '%states%'
or deaths.location like 'Sweden'
or deaths.location like 'Denmark'
or deaths.location like 'Switzerland'
or deaths.location like 'Norway')
Order by InfectionPercentage DESC
--Countries with Highest Rate of Deaths
Select Location, MAX(cast(total_deaths as int)) as TotalDeathCount
From coviddeaths
Where continent is not null
Group by Location
Order by TotalDeathCount DESC
--Locations by Highest Rate of Deaths
Select location, MAX(cast(total_deaths as int)) as TotalDeathCount
From coviddeaths
Where continent is null
Group by location
Order by TotalDeathCount DESC
--Continents with Highest Total Death Count
Select continent, MAX(cast(total_deaths as int)) as TotalDeathCount
From coviddeaths
Where continent is not null
Group by continent
Order by TotalDeathCount DESC
-- GLOBAL NUMBERS
Select date,SUM(new_cases) as total_cases, SUM(cast(new_deaths as int)) as total_deaths, SUM(cast(new_deaths as int))/SUM(new_cases)*100 as DeathPercentage
From coviddeaths
Where continent is not null
Group by date
Order by 1,2
--Join two tables together
Select *
From coviddeaths dea
Join covidvaccinations 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 coviddeaths dea
Join covidvaccinations vac
On dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
order by 1,2,3
--use CTE to calculate rolling vaccintation
With PopvsVac (Continent, Location, Date, Population, new_vaccinations, RollingPeopleVaccinated)
as
(
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations,
SUM(convert(float,vac.new_vaccinations)) OVER (Partition by dea.Location order by dea.location, dea.Date)
as RollingPeopleVaccinated
From coviddeaths dea
Join covidvaccinations vac
On dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
and vac.new_vaccinations is not null
)
Select *, (RollingPeopleVaccinated/Population)*100 AS PercentPeopleVaccinated
From PopvsVac
--TEMP TABLE
DROP TABLE IF exists #PercentVaccinated
Create table #PercentVaccinated
(Continent nvarchar(255), location nvarchar(255), date datetime, population numeric,
new_vaccinations numeric, RollingPeopleVaccinated numeric)
Insert into #PercentVaccinated
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations,
SUM(convert(float,vac.new_vaccinations)) OVER (Partition by dea.Location order by dea.location, dea.Date)
as RollingPeopleVaccinated
From coviddeaths dea
Join covidvaccinations vac
On dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
and vac.new_vaccinations is not null
Select *, (RollingPeopleVaccinated/Population)*100 AS PercentPeopleVaccinated
From #PercentVaccinated
--Creating view to store data for later visualizations
Create View PercentPopulationVaccinated as
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations,
SUM(convert(float,vac.new_vaccinations)) OVER (Partition by dea.Location order by dea.location, dea.Date)
as RollingPeopleVaccinated
From coviddeaths dea
Join covidvaccinations vac
On dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
and vac.new_vaccinations is not null