-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataExplorationWithSQL.sql
More file actions
143 lines (129 loc) · 5.04 KB
/
DataExplorationWithSQL.sql
File metadata and controls
143 lines (129 loc) · 5.04 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
SELECT *
FROM PortfolioProject1..CovidDeaths
ORDER BY 3,4
SELECT *
FROM PortfolioProject1..CovidVaccinations
ORDER BY 3,4
--DATA THAT WILL BE USED
SELECT location, date, total_cases, new_cases, total_deaths, population
FROM PortfolioProject1..CovidDeaths
Where continent is not null
ORDER BY 1,2
--LOOKING AT TOTAL CASES v/s TOTAL DEATHS
SELECT location, date, total_cases, total_deaths, (total_deaths/total_cases)* 100 AS DeathPercentages
FROM PortfolioProject1..CovidDeaths
WHERE location like '%states%' AND continent is not null
ORDER BY 1,2
--Looking at total cases v/s population
SELECT location, date, population, total_cases, (total_cases/population)* 100 AS TotPopulationCases
FROM PortfolioProject1..CovidDeaths
Where continent is not null
--WHERE location like 'G%'
ORDER BY 1,2
--Number of highest effected cases in country as per population
SELECT location, population, MAX(total_cases) AS HighestInfectionCount, MAX((total_cases/population))* 100
AS PercentInfectedPopulation FROM PortfolioProject1..CovidDeaths
Where continent is not null
GROUP BY location, population
ORDER BY PercentInfectedPopulation desc
--Showing countries with highest death counts per population
SELECT location, MAX(cast(total_deaths as int)) AS HighestDeathCount
FROM PortfolioProject1..CovidDeaths
Where continent is not null
GROUP BY location
ORDER BY HighestDeathCount desc
---Removing Null values because there is error while grouping countries
SELECT *
FROM PortfolioProject1..CovidDeaths
Where continent is not null
ORDER BY 3,4
---Let's break the data according to continents
--showing continents with highest death count per population
SELECT continent, MAX(cast(total_deaths as int)) AS HighestDeathCount
FROM PortfolioProject1..CovidDeaths
Where continent is not null
GROUP BY continent
ORDER BY HighestDeathCount desc
--- Global Numbers
SELECT 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 DeathPercentages
FROM PortfolioProject1..CovidDeaths
WHERE continent is not null
---GROUP BY date
ORDER BY 1,2
-- Joining the two tables:
SELECT *
FROM PortfolioProject1..CovidDeaths AS dea
JOIN PortfolioProject1..CovidVaccinations as vac
ON dea.location = vac.location
and dea.date = vac.date
--Looking at total population v/s vaccincations
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
FROM PortfolioProject1..CovidDeaths AS dea
JOIN PortfolioProject1..CovidVaccinations as vac
ON dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
ORDER BY 1,2,3
--Rolling Count
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations,
SUM(cast(vac.new_vaccinations as int)) OVER (Partition by dea.location ORDER BY dea.location, dea.date)
as RollingPeopleVaccinated
FROM PortfolioProject1..CovidDeaths AS dea
JOIN PortfolioProject1..CovidVaccinations as vac
ON dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
ORDER BY 1,2,3
--Using CTE
With Popvsvac (continent, location, date, population, new_vaccinations, RollingPeopleVaccinated)
as
(
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations,
SUM(cast(vac.new_vaccinations as int)) OVER (Partition by dea.location ORDER BY dea.location, dea.date)
as RollingPeopleVaccinated
FROM PortfolioProject1..CovidDeaths AS dea
JOIN PortfolioProject1..CovidVaccinations as vac
ON dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
--ORDER BY 2,3
)
SELECT *, (RollingPeopleVaccinated/population) * 100 as PercentageofRPV
FROM Popvsvac
---TEMP TABLE
DROP TABLE if exists #PercentPopulationVaccinated
CREATE TABLE #PercentPopulationVaccinated
(
continent nvarchar(255),
location nvarchar(255),
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(cast(vac.new_vaccinations as int)) OVER (Partition by dea.location ORDER BY dea.location, dea.date)
as RollingPeopleVaccinated
FROM PortfolioProject1..CovidDeaths AS dea
JOIN PortfolioProject1..CovidVaccinations as vac
ON dea.location = vac.location
and dea.date = vac.date
--where dea.continent is not null
--ORDER BY 2,3
SELECT *, (RollingPeopleVaccinated/population) * 100 as PercentageofRPV
FROM #PercentPopulationVaccinated
-- Creating Views for future visualizations
Create View PercentPopulationVaccinated as
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations,
SUM(cast(vac.new_vaccinations as int)) OVER (Partition by dea.location ORDER BY dea.location, dea.date)
as RollingPeopleVaccinated
FROM PortfolioProject1..CovidDeaths AS dea
JOIN PortfolioProject1..CovidVaccinations as vac
ON dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
--ORDER BY 2,3
SELECT *
FROM PercentPopulationVaccinated