-
Notifications
You must be signed in to change notification settings - Fork 0
/
electricity_gdp_analysis.sql
249 lines (224 loc) · 8.6 KB
/
electricity_gdp_analysis.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
USE test;
DROP DATABASE test;
CREATE DATABASE test;
CREATE TABLE energy_data (
id INT AUTO_INCREMENT PRIMARY KEY,
`Country Name` VARCHAR(100),
`Country Code` VARCHAR(10),
`Series Name` VARCHAR(100),
`Series Code` VARCHAR(10),
`2000` DECIMAL(15, 2),
`2001` DECIMAL(15, 2),
`2002` DECIMAL(15, 2),
`2003` DECIMAL(15, 2),
`2004` DECIMAL(15, 2),
`2005` DECIMAL(15, 2),
`2006` DECIMAL(15, 2),
`2007` DECIMAL(15, 2),
`2008` DECIMAL(15, 2),
`2009` DECIMAL(15, 2),
`2010` DECIMAL(15, 2),
`2011` DECIMAL(15, 2),
`2012` DECIMAL(15, 2),
`2013` DECIMAL(15, 2),
`2014` DECIMAL(15, 2),
`2015` DECIMAL(15, 2),
`2016` DECIMAL(15, 2),
`2017` DECIMAL(15, 2),
`2018` DECIMAL(15, 2),
`2019` DECIMAL(15, 2),
`2020` DECIMAL(15, 2)
);
SELECT COUNT(*) FROM energy_data; --Query to count total records in energy_data
--check for null values
SELECT COUNT(*) AS NULL_Country_Name FROM energy_data WHERE `Country Name` IS NULL;
--Retrieve specific year data from energy_data
SELECT `2000` FROM energy_data WHERE `2000` IS NOT NULL;
DESCRIBE energy_data
--Calculate yearly averages for Access to electricity (% of population) from energy_data
SELECT
AVG(`2000`) AS avg_2000,
AVG(`2005`) AS avg_2005,
AVG(`2010`) AS avg_2010,
AVG(`2015`) AS avg_2015,
AVG(`2020`) AS avg_2020
FROM energy_data
WHERE `Series Name` = 'Access to electricity (% of population)';
--Calculate yearly averages for CO2 emissions (metric tons per capita) from energy_data
SELECT
AVG(`2000`) AS avg_2000,
AVG(`2005`) AS avg_2005,
AVG(`2010`) AS avg_2010,
AVG(`2015`) AS avg_2015,
AVG(`2020`) AS avg_2020
FROM energy_data
WHERE `Series Name` = 'CO2 emissions (metric tons per capita)';
--Calculate yearly averages for gross capital formation (% of GDP) from energy_data
SELECT
AVG(`2000`) AS avg_2000,
AVG(`2005`) AS avg_2005,
AVG(`2010`) AS avg_2010,
AVG(`2015`) AS avg_2015,
AVG(`2020`) AS avg_2020
FROM energy_data
WHERE `Series Name` = 'Gross capital formation (% of GDP)';
--Calculate yearly averages for CO2 emissions from electricity and heat production from energy_data
SELECT
AVG(`2000`) AS avg_2000,
AVG(`2005`) AS avg_2005,
AVG(`2010`) AS avg_2010,
AVG(`2015`) AS avg_2015,
AVG(`2020`) AS avg_2020
FROM energy_data
WHERE `Series Name` = 'CO2 emissions from electricity and heat production, total (% of total fuel combustion)';
--Create filtered_data CTE to aggregate key indicators for 2000: electricity access, GDP, and CO2 emissions
WITH filtered_data AS (
SELECT
`Country Name`, -- The name of the country
`Series Name`, -- The name of the series (indicator)
CASE
-- Capture electricity access data for the year 2000
WHEN `Series Name` = 'Access to electricity (% of population)' THEN `2000`
ELSE NULL
END AS ELEC_2000, -- Electricity access for 2000
CASE
-- Capture GDP data for the year 2000
WHEN `Series Name` = 'Gross capital formation (% of GDP)' THEN `2000`
ELSE NULL
END AS GDP_2000, -- GDP for 2000
CASE
-- Capture CO2 emissions data for the year 2000
WHEN `Series Name` = 'CO2 emissions (metric tons per capita)' THEN `2000`
ELSE NULL
END AS CO2_Emissions_2000 -- CO2 emissions for 2000
FROM energy_data -- Source table
WHERE `Series Name` IN (
-- Filter for relevant series of interest
'Access to electricity (% of population)',
'CO2 emissions (metric tons per capita)',
'Gross capital formation (% of GDP)'
)
)
-- returns the max value for electricity, gdp, and co2 emissions for each country
SELECT
`Country Name`,
MAX(ELEC_2000) AS MAXELEC_2000,
MAX(GDP_2000) AS MAXGDP_2000,
MAX(CO2_Emissions_2000) AS MAXCO2_2000
FROM
filtered_data
GROUP BY
`Country Name`;
--Extract CO2 emissions (metric tons per capita) for the year 2000 from energy_data.
SELECT
`Country Name`,
`2000` AS Year,
CASE WHEN `Series Name` = 'CO2 emissions (metric tons per capita)' THEN `2000` END AS CO2_Emissions
FROM energy_data
WHERE `Series Name` = 'CO2 emissions (metric tons per capita)';
--electricitty access data
CREATE VIEW electricity_access_data AS
SELECT
`Country Name`,
`2000` AS Year_2000,
`2005` AS Year_2005,
`2010` AS Year_2010,
`2015` AS Year_2015,
`2020` AS Year_2020
FROM
energy_data
WHERE
`Series Name` = 'Access to electricity (% of population)';
SELECT * FROM electricity_access_data;
--gdp data
CREATE VIEW gdp_data AS
SELECT
`Country Name`,
`2000` AS Year_2000,
`2005` AS Year_2005,
`2010` AS Year_2010,
`2015` AS Year_2015,
`2020` AS Year_2020
FROM
energy_data
WHERE
`Series Name` = 'Gross capital formation (% of GDP)';
--c02 data
CREATE VIEW co2_data AS
SELECT
`Country Name`,
`2000` AS Year_2000,
`2005` AS Year_2005,
`2010` AS Year_2010,
`2015` AS Year_2015,
`2020` AS Year_2020
FROM
energy_data
WHERE
`Series Name` = 'CO2 emissions (metric tons per capita)';
-- Effect on Economic Growth:
-- This view analyzes the relationship between improvements in electricity access
-- and GDP growth for various countries over the years.
CREATE VIEW electricity_gdp_analysis AS
SELECT
e.`Country Name`, -- Name of the country
e.`Year_2000`, -- Electricity access percentage in the year 2000
e.`Year_2020` AS `Electricity_Access_2020`, -- Electricity access percentage in the year 2020
(e.`Year_2020` - e.`Year_2000`) AS `Electricity_Access_Improvement`, -- Improvement in electricity access from 2000 to 2020
g.`Year_2000` AS `GDP_2000`, -- GDP percentage in the year 2000
g.`Year_2020` AS `GDP_2020`, -- GDP percentage in the year 2020
(g.`Year_2020` - g.`Year_2000`) AS `GDP_Growth` -- GDP growth from 2000 to 2020
FROM
electricity_access_data e -- Using electricity access data
JOIN
gdp_data g ON e.`Country Name` = g.`Country Name`; -- Joining with GDP data based on country name
-- Selecting all records from the electricity_gdp_analysis view
SELECT * FROM electricity_gdp_analysis;
-- This query categorizes countries based on the improvement in electricity access
-- and calculates the average GDP growth for each category.
SELECT
CASE
WHEN `Electricity_Access_Improvement` > 20 THEN 'Significant Improvement' -- Categorizes as significant if improvement is over 20%
WHEN `Electricity_Access_Improvement` BETWEEN 0 AND 20 THEN 'Moderate Improvement' -- Categorizes as moderate for 0-20% improvement
ELSE 'No Improvement' -- Categorizes as no improvement if there is no increase
END AS Improvement_Category, -- Naming the resulting category column
AVG(`GDP_Growth`) AS Average_GDP_Growth -- Calculating average GDP growth for the grouped categories
FROM
electricity_gdp_analysis -- Using the electricity_gdp_analysis view created earlier
GROUP BY
Improvement_Category; -- Grouping results by improvement category
-- Creating a view that combines electricity access, GDP, and CO2 emissions data for each country over multiple years.
CREATE VIEW combined_trends AS
SELECT
e.`Country Name`,
e.`Year_2000` AS Electricity_Access_2000,
e.`Year_2005` AS Electricity_Access_2005,
e.`Year_2010` AS Electricity_Access_2010,
e.`Year_2015` AS Electricity_Access_2015,
e.`Year_2020` AS Electricity_Access_2020,
g.`Year_2000` AS GDP_2000,
g.`Year_2005` AS GDP_2005,
g.`Year_2010` AS GDP_2010,
g.`Year_2015` AS GDP_2015,
g.`Year_2020` AS GDP_2020,
c.`Year_2000` AS CO2_2000,
c.`Year_2005` AS CO2_2005,
c.`Year_2010` AS CO2_2010,
c.`Year_2015` AS CO2_2015,
c.`Year_2020` AS CO2_2020
FROM
electricity_access_data e -- Using the electricity access data view
JOIN
JOIN
gdp_data g ON e.`Country Name` = g.`Country Name` -- Joining with GDP data based on country name
JOIN
co2_data c ON e.`Country Name` = c.`Country Name`; -- Joining with CO2 data based on country name
SELECT * FROM combined_trends;-- Selecting all data from the combined trends view
-- Calculating percentage changes in electricity access, GDP, and CO2 emissions from the year 2000 to 2020 for each country.
SELECT
`Country Name`, -- Selecting the country name
(Electricity_Access_2020 - Electricity_Access_2000) / Electricity_Access_2000 * 100 AS Electricity_Access_Change, -- Calculating the percentage change in electricity access
(GDP_2020 - GDP_2000) / GDP_2000 * 100 AS GDP_Change, -- Calculating the percentage change in GDP
(CO2_2020 - CO2_2000) / CO2_2000 * 100 AS CO2_Change -- Calculating the percentage change in CO2 emissions
FROM
combined_trends; -- Using the combined trends view to retrieve the necessary data