-
Notifications
You must be signed in to change notification settings - Fork 0
/
COVID_19.sql
128 lines (113 loc) · 6.01 KB
/
COVID_19.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
--Viewing all the columns from CovidDeaths Table
select *
from PortFolioProject.dbo.CovidDeaths
order by 3,4
--------------------------------------------------------------------------------------------------------------------------
--Viewing all the columns from CovidVaccination Table
select *
from PortFolioProject.dbo.CovidVaccination
order by 3,4
--------------------------------------------------------------------------------------------------------------------------
--View specific columns from CovidDeaths Table
select location, date,total_cases,new_cases,total_deaths,population
from PortFolioProject.dbo.CovidDeaths
order by 1,2
--------------------------------------------------------------------------------------------------------------------------
--Looking at Total cases vs total Deaths as Percentage for all countries
--Shows Likelihood of dying if you contact of Covid in Canada
select location, date,total_cases,round((total_deaths/total_cases)*100,2) as DeathPercentage
from PortFolioProject.dbo.CovidDeaths
where total_cases !=0 and location like 'canada%'
order by 1,2
--------------------------------------------------------------------------------------------------------------------------
--Looking at Total cases vs population as percentage in Canada
select location, date,total_cases,population,round((total_cases/population)*100,2) as PercentPopulationinfected
from PortFolioProject.dbo.CovidDeaths
where total_cases !=0 and location like 'canada%'
order by 1,2
--------------------------------------------------------------------------------------------------------------------------
--Looking at countries with highest infection rates compared to population
select location, population,max(total_cases) as HighestInfectionCount,max(round((total_cases/population)*100,2)) as PercentPopulationInfected
from PortFolioProject.dbo.CovidDeaths
where total_cases !=0
group by location, population
order by PercentPopulationInfected desc
--------------------------------------------------------------------------------------------------------------------------
--Looking for countries with highest death count per population
select location,max(Total_Deaths) as TotalDeathCount
from PortFolioProject.dbo.CovidDeaths
where total_cases !=0 and continent is not null
group by location
order by TotalDeathCount desc
--------------------------------------------------------------------------------------------------------------------------
--Looking for Continents with highest death count per population
select continent,max(Total_Deaths) as TotalDeathCount
from PortFolioProject.dbo.CovidDeaths
where total_cases !=0 and continent is not null
group by continent
order by TotalDeathCount desc
--------------------------------------------------------------------------------------------------------------------------
--Looking for Global Numbers
select sum(new_cases) as TotalCases,sum(new_deaths) as TotalDeaths,round((sum(new_deaths)/sum(new_cases))*100,2) as DeathPercentage
from PortFolioProject.dbo.CovidDeaths
where new_cases !=0 and continent is not null
order by 1,2
--------------------------------------------------------------------------------------------------------------------------
--looking total Population vs vaccinations by joining two tables w.r.t Location and Date
select d.continent,d.location,d.date,d.population,v.new_vaccinations,
sum(v.new_vaccinations) over (partition by d.location order by d.location,d.date) as RollingVaccinationCount
--(RollingVaccinationCount/d.population)*100
from PortFolioProject.dbo.CovidDeaths d
join PortFolioProject.dbo.CovidVaccinations v
on d.location=v.location
and d.date=v.date
where d.continent is not null and v.new_vaccinations is not null
order by 2,3
---------------------------------------------------------------------------------------------------------------------------
--looking total Population vs vaccinations by joining two tables w.r.t Location and Date and Using CTE
with PopvsVac (Continent,location,date,population,New_Vaccinations,RollingVaccinationCount)
as
(
select d.continent,d.location,d.date,d.population,v.new_vaccinations,
sum(v.new_vaccinations) over (partition by d.location order by d.location,d.date) as RollingVaccinationCount
from PortFolioProject.dbo.CovidDeaths d
join PortFolioProject.dbo.CovidVaccinations v
on d.location=v.location
and d.date=v.date
where d.continent is not null and v.new_vaccinations is not null)
select * ,(RollingVaccinationCount/population)*100 as RollingPercentage
from PopvsVac
--------------------------------------------------------------------------------
--TEMP Tables
drop table if exists #PercentagePopulationVaccinated
create table #PercentagePopulationVaccinated
(
continent nvarchar(255),
location nvarchar(255),
date datetime,
population numeric,
new_vaccinations numeric,
RollingVaccinationCount numeric
)
insert into #PercentagePopulationVaccinated
select d.continent,d.location,d.date,d.population,v.new_vaccinations,
sum(v.new_vaccinations) over (partition by d.location order by d.location,d.date) as RollingVaccinationCount
from PortFolioProject.dbo.CovidDeaths d
join PortFolioProject.dbo.CovidVaccinations v
on d.location=v.location
and d.date=v.date
where d.continent is not null and v.new_vaccinations is not null
select * ,(RollingVaccinationCount/population)*100 as RollingPercentage
from #PercentagePopulationVaccinated
------------------------------------------------------------------------------------------
--views
drop view if exists PercentagePopulationVaccinated
create view PercentagePopulationVaccinated as
select d.continent,d.location,d.date,d.population,v.new_vaccinations,
sum(v.new_vaccinations) over (partition by d.location order by d.location,d.date) as RollingVaccinationCount
from PortFolioProject.dbo.CovidDeaths d
join PortFolioProject.dbo.CovidVaccinations v
on d.location=v.location
and d.date=v.date
where d.continent is not null and v.new_vaccinations is not null
select * from PercentagePopulationVaccinated