-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLQuery_1.sql
179 lines (142 loc) · 4.65 KB
/
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
167
168
169
170
171
172
173
174
175
176
177
178
179
-- Just reading data.
SELECT *
from CovidDeaths
where continent is not null
order by 3, 4
-- Select Data to be used
SELECT Location, Date, total_cases, new_cases, total_deaths, population
from CovidDeaths
where continent is not null
order by 1 DESC
-- Total cases vs total deaths.
-- Shows the chances of person dying due to covid.
SELECT location, date, total_cases, total_deaths, (total_deaths/total_cases) * 100 as Death_Percentage
FROM CovidDeaths
where location = 'India'
and continent is not null
ORDER by 2
-- Total cases vs population
-- Shows the infected percentage as per the population.
Select Location, Date, population, total_cases, (total_cases/population) * 100 as Infected_Percentage
FROM CovidDeaths
where location = 'Germany'
and continent is not null
order by 2
-- Countries with highest infection rate compared to the population
Select location, population, max(total_cases) as Highest_Infection_Count, max((total_cases/population)) * 100 as Max_Infected_Percentage
FROM CovidDeaths
where continent is not null
group by location, population
ORDER by 4 desc
-- Continents with highest infection rate
Select continent, max(total_cases) as Highest_Infection_Rate, max(total_cases/population) * 100 as Max_Infected_Percentage
From CovidDeaths
where continent is not null
group by continent
-- Countries with Highest Death Count
Select location, max(total_deaths) as Max_Death_Count
From CovidDeaths
where continent is null
group by location
order by 2 desc
-- Continents with highest death rate
SELECT continent, max(total_deaths) as Max_Death_Count
From CovidDeaths
GROUP by continent
order by 2
-- Sum of new cases grouped by date at global level.
SELECT Date,
sum(new_cases) as New_Cases_Sum,
sum(new_deaths) as New_Deaths_Sum,
format(SUM(new_deaths)/SUM(new_cases) * 100, 'N3') as Death_Percentage
From CovidDeaths
where continent is not null
group by date
order by 1
-- Across the world.
SELECT
sum(new_cases) as New_Cases_Sum,
sum(new_deaths) as New_Deaths_Sum,
format(SUM(new_deaths)/SUM(new_cases) * 100, 'N3') as Death_Percentage
From CovidDeaths
where continent is not null
order by 1
-- Calculating the rolled vaccinations as per date, location
SELECT dea.[Date],
dea.Continent,
dea.[Location],
dea.Population,
vac.New_vaccinations,
SUM(vac.new_vaccinations) over (partition by dea.location order by dea.Date, dea.location) as Rolled_Vaccinations
From CovidDeaths as dea
JOIN CovidVaccinations as vac
on dea.date = vac.[date]
and dea.[location] = vac.[location]
WHERE dea.continent is NOT NULL
order by 3
-- With CTE
WITH PopsvsVacc (Date, Continent, Location, Population, New_vaccinations, Rolled_Vaccinations)
as
(
SELECT dea.[Date],
dea.Continent,
dea.[Location],
dea.Population,
vac.New_vaccinations,
SUM(vac.new_vaccinations) over (partition by dea.location order by dea.Date, dea.location) as Rolled_Vaccinations
From CovidDeaths as dea
JOIN CovidVaccinations as vac
on dea.date = vac.[date]
and dea.[location] = vac.[location]
WHERE dea.continent is NOT NULL
)
Select *, (Rolled_Vaccinations/Population) * 100 as Vaccinated_Percentage
FROM PopsvsVacc
SELECT SUM(new_deaths)
FROM CovidDeaths
WHERE continent is not NULL
and continent in ('Asia', 'Europe', 'Africa', 'South America', 'North America', 'Oceania')
and [location] != 'World'
Select max(date)
from CovidDeaths
-- With Temp tables
DROP TABLE if EXISTS #VaccinationTemp
CREATE table #VaccinationTemp
(
Date DATE,
Continent VARCHAR(255),
Location VARCHAR(255),
Population NUMERIC,
New_vaccinations NUMERIC,
Rolled_Vaccinations NUMERIC
)
Insert into #VaccinationTemp
SELECT dea.[Date],
dea.Continent,
dea.[Location],
dea.Population,
vac.New_vaccinations,
SUM(vac.new_vaccinations) over (partition by dea.location order by dea.Date, dea.location) as Rolled_Vaccinations
From CovidDeaths as dea
JOIN CovidVaccinations as vac
on dea.date = vac.[date]
and dea.[location] = vac.[location]
-- WHERE dea.continent is NOT NULL
SELECT *, (Rolled_Vaccinations/Population) * 100 as Vaccinated_Percentage
FROM #VaccinationTemp
-- Creating views
Create view VaccinatedPeopleView AS
SELECT dea.[Date],
dea.Continent,
dea.[Location],
dea.Population,
vac.New_vaccinations,
SUM(vac.new_vaccinations) over (partition by dea.location order by dea.Date, dea.location) as Rolled_Vaccinations
From CovidDeaths as dea
JOIN CovidVaccinations as vac
on dea.date = vac.[date]
and dea.[location] = vac.[location]
WHERE dea.continent is NOT NULL
-- order by 3
SELECT *
from VaccinatedPeopleView