-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_sql_commands.sql
More file actions
274 lines (233 loc) · 9.12 KB
/
db_sql_commands.sql
File metadata and controls
274 lines (233 loc) · 9.12 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
-- Query 1:
SELECT e.kg
FROM electricity_per_capita e, country_code c
WHERE c.country_name = “India” AND c.country_code = e.country_code AND e.Year = 1985;
-- Query 2:
SELECT DISTINCT p.population*e.kg AS total_electricity, p.country_code, p.year
FROM country_population p, electricity_per_capita e, country_code c
WHERE p.country_code = e.country_code and p.year = e.year and p.country_code = "USA" AND p.year = 2000;
-- Query 3:
SELECT AVG(e.Percentage)
FROM electricity_from_renewable_percentage e, country_code c
WHERE c.country_name = “Germany” AND c.country_code = e.country_code AND e.Year >= 1999 AND e.Year <= 2014;
--- Query 4:
CREATE VIEW country1_resource1
AS
SELECT DISTINCT year, kg
FROM energy_per_thousand_dollars as e, country_code as c
WHERE e.country_code = c.country_code AND c.country_name = "tanzania" AND year <= 2010 AND year >= 2005
;
CREATE VIEW country2_resource2
AS
SELECT DISTINCT year, percentage
FROM access_to_electricity_percentage as e, country_code as c
WHERE e.country_code = c.country_code AND c.country_name = "Tanzania" AND year <= 2010 AND year >= 2005
;
SELECT
AVG((s.kg - (SELECT AVG(s.kg)
FROM country1_resource1 s))*(i.percentage - (SELECT AVG(i.percentage)
FROM country2_resource2 i)))/((SELECT STDDEV(s1.kg)
from country1_resource1 s1)*(SELECT STDDEV(s2.percentage)
FROM country2_resource2 s2))
FROM country1_resource1 as s, country2_resource2 as i
WHERE s.year = i.year;
drop view if exists country1_resource1, country2_resource2;
-- Query 5:
CREATE VIEW country1_resource1
AS
SELECT DISTINCT year, kg
FROM electricity_per_capita as e, country_code as c
WHERE e.country_code = c.country_code AND c.country_name = "switzerland" AND year <= 2010 AND year >= 2000
;
CREATE VIEW country2_resource2
AS
SELECT DISTINCT year, kg
FROM electricity_per_capita as e, country_code as c
WHERE e.country_code = c.country_code AND c.country_name = "italy" AND year <= 2010 AND year >= 2000
;
SELECT
AVG((s.kg - (SELECT AVG(s.kg)
FROM country1_resource1 s))*(i.kg - (SELECT AVG(i.kg)
FROM country2_resource2 i)))/((SELECT STDDEV(s1.kg)
from country1_resource1 s1)*(SELECT STDDEV(s2.kg)
FROM country2_resource2 s2))
FROM country1_resource1 as s, country2_resource2 as i
WHERE s.year = i.year;
drop view if exists country1_resource1, country2_resource2;
--- Query 6:
SELECT c.country_name
FROM country_code c, electricity_from_renewable_percentage r
WHERE c.country_code = r.country_code AND r.year = 2000
ORDER BY r.percentage DESC
LIMIT 5;
--- Query 7:
SELECT c.country_name
FROM country_code c, electricity_from_renewable_percentage r
WHERE c.country_code = r.country_code
GROUP BY r.country_code
ORDER BY AVG(r.percentage) DESC
LIMIT 5;
-- Query 8:
SELECT c.country_name
FROM country_code c, electricity_from_renewable_percentage r
WHERE c.country_code = r.country_code AND r.year >= 2013 AND r.year < 2018
GROUP BY r.country_code
ORDER BY AVG(r.Percentage) DESC
Limit 5;
-- Query 9:
SELECT DISTINCT c.country_name
FROM country_code c, electricity_from_renewable_percentage r, electricity_from_coal_percentage cl, electricity_from_oil_percentage o
WHERE c.country_code = r.country_code AND r.year >= 1988 AND r.year < 2018 AND cl.country_code = r.country_code AND o.country_code = r.country_code AND r.year = cl.year AND cl.year = o.year
GROUP BY c.country_code
HAVING AVG(cl.percentage) + AVG(o.percentage) < AVG(r.percentage);
-- Query 10:
CREATE VIEW avg_renewable_energy
AS
SELECT Year, AVG(Percentage) as avg_energy
FROM electricity_from_renewable_percentage
GROUP BY Year;
SELECT AVG((r.avg_energy - (SELECT AVG(r.avg_energy)
FROM avg_renewable_energy r))*(p.real_value_dollar - (SELECT AVG(p.real_value_dollar)
FROM usa_crude_oil_prices p)))/((SELECT STDDEV(r.avg_energy)
FROM avg_renewable_energy r)*(SELECT STDDEV(p.real_value_dollar)
FROM usa_crude_oil_prices p))
FROM avg_renewable_energy r, usa_crude_oil_prices p
WHERE r.year = p.year;
DROP VIEW avg_renewable_energy;
--- Query 11:
CREATE VIEW total_energy_global
AS
SELECT e.year, SUM(e.kg*p.population) as total_energy
FROM electricity_per_capita e, country_population p
WHERE e.country_code = p.country_code AND e.year = p.year
GROUP BY e.year;
SELECT AVG((r.total_energy - (SELECT AVG(r.total_energy)
FROM total_energy_global r))*(p.real_value_dollar - (SELECT AVG(p.real_value_dollar)
FROM usa_crude_oil_prices p)))/((SELECT STDDEV(r.total_energy)
FROM total_energy_global r)*(SELECT STDDEV(p.real_value_dollar)
FROM usa_crude_oil_prices p))
FROM total_energy_global r, usa_crude_oil_prices p
WHERE r.year = p.year;
DROP VIEW total_energy_global;
--- Query 12:
CREATE VIEW total_energy_global
AS
SELECT e.year, SUM(e.kg*p.population) as total_energy
FROM electricity_per_capita e, country_population p, electricity_from_coal_percentage cl, electricity_from_oil_percentage o
WHERE e.country_code = p.country_code AND e.year = p.year AND cl.country_code = e.country_code AND o.country_code = cl.country_code AND o.year = cl.year AND o.year = e.year
GROUP BY e.year
HAVING AVG(cl.percentage) + AVG(o.percentage) > 70;
SELECT AVG((r.total_energy - (SELECT AVG(r.total_energy)
FROM total_energy_global r))*(p.real_value_dollar - (SELECT AVG(p.real_value_dollar)
FROM usa_crude_oil_prices p)))/((SELECT STDDEV(r.total_energy)
FROM total_energy_global r)*(SELECT STDDEV(p.real_value_dollar)
FROM usa_crude_oil_prices p))
FROM total_energy_global r, usa_crude_oil_prices p
WHERE r.year = p.year;
DROP VIEW total_energy_global;
-------------------------CREATE TABLE COMMANDS---------------------------------------
CREATE TABLE IF NOT EXISTS
access_to_electricity_percentage (
country_code varchar(5) NOT NULL,
year INT NOT NULL,
percentage FLOAT CHECK(percentage>=0 AND percentage <=100),
id INT(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS
electricity_from_renewable_percentage (
country_code varchar(5) NOT NULL,
year INT NOT NULL,
percentage FLOAT CHECK(percentage>=0 AND percentage <=100),
id INT(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
DROP TABLE electricity_from_coal_percentage;
CREATE TABLE IF NOT EXISTS
electricity_from_coal_percentage (
country_code varchar(5) NOT NULL,
year INT NOT NULL,
percentage FLOAT CHECK(percentage>=0 AND percentage <=100),
id INT(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
DROP TABLE electricity_from_nuclear_percentage;
CREATE TABLE IF NOT EXISTS
electricity_from_nuclear_percentage (
country_code varchar(5) NOT NULL,
year INT NOT NULL,
percentage FLOAT CHECK(percentage>=0 AND percentage <=100),
id INT(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
DROP TABLE electricity_from_oil_percentage;
CREATE TABLE IF NOT EXISTS
electricity_from_oil_percentage (
country_code varchar(5) NOT NULL,
year INT NOT NULL,
percentage FLOAT CHECK(percentage>=0 AND percentage <=100),
id INT(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
DROP TABLE country_population;
CREATE TABLE IF NOT EXISTS
country_population (
country_code varchar(5) NOT NULL,
year INT NOT NULL,
population INT,
id INT(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
DROP TABLE electricity_per_capita;
CREATE TABLE IF NOT EXISTS
electricity_per_capita (
country_code varchar(5) NOT NULL,
year INT NOT NULL,
kg FLOAT,
id INT(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
DROP TABLE energy_per_thousand_dollars;
CREATE TABLE IF NOT EXISTS
energy_per_thousand_dollars (
country_code varchar(5) NOT NULL,
year INT NOT NULL,
kg FLOAT,
id INT(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS
country_metadata (
country_code varchar(5) NOT NULL UNIQUE,
region varchar(50) NOT NULL,
IncomeGroup varchar(50) NOT NULL,
PRIMARY KEY (country_code)
);
DROP TABLE world_happiness_index;
CREATE TABLE IF NOT EXISTS
world_happiness_index (
country varchar(50) NOT NULL UNIQUE,
happiness_score FLOAT CHECK(happiness_score>0 AND happiness_score <10),
life_expectancy FLOAT CHECK(life_expectancy>0 AND life_expectancy <1),
government_corruption FLOAT CHECK(government_corruption>0 AND government_corruption <1),
PRIMARY KEY (country)
);
CREATE TABLE IF NOT EXISTS
country_code (
country_name varchar(50) NOT NULL UNIQUE,
country_code varchar(5) NOT NULL UNIQUE,
PRIMARY KEY (country_code)
);
CREATE TABLE IF NOT EXISTS
usa_crude_oil_prices (
year INT NOT NULL UNIQUE,
real_value_dollar FLOAT NOT NULL,
nominal_value_dollar FLOAT NOT NULL,
PRIMARY KEY (year)
);
CREATE TABLE IF NOT EXISTS
Imported_Crude_Oil_Prices (
year INT NOT NULL UNIQUE,
real_value_dollar FLOAT NOT NULL,
PRIMARY KEY (year)
);