-
Notifications
You must be signed in to change notification settings - Fork 0
/
Covid_SQLQuery_1.sql
167 lines (120 loc) · 4.95 KB
/
Covid_SQLQuery_1.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
/*
Covid 19 Data Exploration
Skill Used: Joins, CTE's, Temp Tables, Windows Functions, Aggregate Functions, Creating Views, Converting Data Types
*/
SELECT * FROM [dbo].[ CovidDeaths01]
SELECT * FROM [dbo].[ CovidVaccinations01 ]
-- select data that we are going to be starting with
SELECT location, date,total_cases, new_cases, total_deaths, population
FROM [dbo].[ CovidDeaths01]
WHERE continent IS NOT NULL
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, (1.0 * total_deaths/total_cases)*100 as DeathPercentage
FROM [dbo].[ CovidDeaths01]
WHERE location LIKE '%India%'
ORDER BY 1,2
-- Looking at Total Cases Vs Population in India
-- Shows what Percentage of Population got Covid
SELECT location, date,total_cases, population, (1.0 * total_cases/Population)*100 AS PercentPopulationInfected
FROM [dbo].[ CovidDeaths01]
-- WHERE location LIKE '%India%'
ORDER BY 1,2
--Looking at countries with highest infection rate compared to population
SELECT location, population , Max(total_cases) AS HighestInfectionCount ,Max(1.0 * total_cases/Population)*100 AS PercentPopulationInfected
FROM [dbo].[ CovidDeaths01]
-- WHERE location LIKE '%India%'
GROUP BY LOCATION, Population
ORDER BY PercentPopulationInfected DESC
-- Showing Countries with Highest Death Count Per Populatipon
SELECT location, MAX(total_deaths) AS TotalDeathCount
FROM [dbo].[ CovidDeaths01]
-- WHERE location LIKE '%India%'
WHERE continent IS NOT NULL
GROUP BY LOCATION
ORDER BY TotalDeathCount DESC
-- Let's Look at The Continent
-- Showing the Continents with the Highest Death Counts per Population
SELECT continent, MAX(total_deaths) AS TotalDeathCount
FROM [dbo].[ CovidDeaths01]
-- WHERE location LIKE '%India%'
WHERE continent IS NOT NULL
GROUP BY continent
ORDER BY TotalDeathCount DESC
-- Global Numbers
SELECT date, SUM(new_cases) AS Total_cases, SUM(new_deaths) AS Total_deaths, SUM(1.0*new_Deaths)/SUM(new_cases)*100 AS DeathPercentage
FROM [dbo].[ CovidDeaths01]
WHERE continent IS NOT NULL
GROUP BY DATE
ORDER BY 1,2
-- Aggregate
SELECT SUM(new_cases) AS Total_cases, SUM(new_deaths) AS Total_deaths, SUM(1.0*new_Deaths)/SUM(new_cases)*100 AS DeathPercentage
FROM [dbo].[ CovidDeaths01]
WHERE continent IS NOT NULL
-- GROUP BY DATE
ORDER BY 1,2
-- Looking at Total Population Vs Total Vaccintaions
-- Shows Percentage of Population that has recieved at least one Covid Vaccine
-- Join Both Table
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations,
SUM(CONVERT(int,vac.new_vaccinations)) OVER (PARTITION BY dea.location ORDER BY dea.date) AS RollingPeopleVaccinated
--(RollingPeopleVaccinated/Population)*100
FROM [dbo].[ CovidDeaths01] dea
JOIN [dbo].[ CovidVaccinations01] vac
ON dea.date = vac.date
AND dea.location = vac.location
WHERE dea.continent IS NOT NULL
ORDER BY 2,3
-- Using CTE to perform Calculation on Partition By in previous query
WITH PopvsVac (continent, loacation, date, population, new_vaccinations, RollingPeopleVaccinated)
AS
(
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations,
SUM(CONVERT(int,vac.new_vaccinations)) OVER (PARTITION BY dea.location ORDER BY dea.date) AS RollingPeopleVaccinated
--(RollingPeopleVaccinated/Population)*100
FROM [dbo].[ CovidDeaths01] dea
JOIN [dbo].[ CovidVaccinations01] vac
ON dea.date = vac.date
AND dea.location = vac.location
WHERE dea.continent IS NOT NULL
-- ORDER BY 2,3
)
SELECT *, (1.0 * RollingPeopleVaccinated/Population)*100
FROM PopvsVac
-- Using Temp Table to perform Calculation on Partition By in previous query
DROP TABLE IF EXISTS #PercentPopulationVaccinated
CREATE TABLE #PercentPopulationVaccinated
(
continent NVARCHAR(250),
Location NVARCHAR(250),
Date Datetime ,
Population NUMERIC,
new_vaccinations NUMERIC,
RollingPeopleVaccinated NUMERIC
)
INSERT INTO #PercentPopulationVaccinated
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations,
SUM(CONVERT(int,vac.new_vaccinations)) OVER (PARTITION BY dea.location ORDER BY dea.date) AS RollingPeopleVaccinated
--(RollingPeopleVaccinated/Population)*100
FROM [dbo].[ CovidDeaths01] dea
JOIN [dbo].[ CovidVaccinations01] vac
ON dea.date = vac.date
AND dea.location = vac.location
-- WHERE dea.continent IS NOT NULL
-- ORDER BY 2,3
SELECT *, (1.0 * RollingPeopleVaccinated/Population)*100
FROM #PercentPopulationVaccinated
-- 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(int,vac.new_vaccinations)) OVER (PARTITION BY dea.location ORDER BY dea.date) AS RollingPeopleVaccinated
--(RollingPeopleVaccinated/Population)*100
FROM [dbo].[ CovidDeaths01] dea
JOIN [dbo].[ CovidVaccinations01] vac
ON dea.date = vac.date
AND dea.location = vac.location
WHERE dea.continent IS NOT NULL
-- ORDER BY 2,3
SELECT *
FROM PercentPopulationVaccinated