Repository navigation
Expand file tree
/
Copy pathpostgres_queries.sql
More file actions
431 lines (361 loc) · 8.71 KB
/
Copy pathpostgres_queries.sql
File metadata and controls
431 lines (361 loc) · 8.71 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
-- PostgreSQL queries
-- By JORDAN DOMINGUEZ & ERIKA PINGATORE
-- December 18, 2017
-- Show all data
SELECT
*
FROM
country;
-- Show POP of USA
SELECT
code,
name,
population
FROM
country
WHERE
code = 'USA'
-- Show SURFACE AREA of USA
SELECT
code,
name,
surfacearea
FROM
country
WHERE
code = 'USA'
-- List all African Countries w/ Pop < 30,000,000 && Life Expectancy > 45
SELECT
code,
name,
continent,
population,
lifeexpectancy
FROM
country
WHERE
continent = 'Africa'
AND
population < 30000000
AND
lifeexpectancy > 45.0;
-- Countries that are "something like" a republic
SELECT
code,
country,
governmentform
FROM
country
WHERE
governmentform LIKE '%Republic%';
-- Something like a republic, indepyear after 1945
SELECT
code,
country,
governmentform,
indepyear
FROM
country
WHERE
governmentform LIKE '%Republic%'
AND
indepyear > 1945;
-- Not like a republic, indepyear after 1945
SELECT
code,
country,
governmentform,
indepyear
FROM
country
WHERE
NOT(governmentform LIKE '%Republic%')
AND
indepyear > 1945;
-- LOWEST LIFE Expectancy of COUNTRIES
SELECT
code,
name,
lifeexpectancy
FROM
country
ORDER BY
lifeexpectancy ASC
LIMIT
15;
-- Highest life expectancy
SELECT
code,
name,
lifeexpectancy
FROM
country
WHERE
lifeexpectancy IS NOT NULL
ORDER BY
lifeexpectancy DESC
LIMIT
15;
-- Lowest/Highest Pop Density
SELECT
code,
name,
population / surfacearea AS pop_density
FROM
country
WHERE
population / surfacearea != 0
ORDER BY
pop_density ASC
LIMIT
5;
-- 'Greenland'
-- 'Svalbard and Jan Mayen'
-- 'Falkland Islands'
-- 'Pitcairn'
-- 'Western Sahara'
SELECT
code,
name,
population / surfacearea AS pop_density
FROM
country
ORDER BY
pop_density DESC
LIMIT
5;
-- 'Macao'
-- 'Monaco'
-- 'Hong Kong'
-- 'Singapore'
-- 'Gibraltar'
-- Smallest 10 COUNTRIES
SELECT code, name, surfacearea, population, surfacearea * population AS area_pop
FROM country
WHERE population !=0
ORDER BY area_pop
LIMIT 10
-- Largest 10 countries
SELECT code, name, surfacearea, population, surfacearea * population AS area_pop
FROM country
WHERE population !=0
ORDER BY area_pop DESC
LIMIT 10
-- using WITH, smallest 10 countries w/ biggest GNP
WITH smallest_countries AS(
SELECT code, name, surfacearea, population, gnp, surfacearea * population AS area_pop
FROM country
WHERE population !=0
ORDER BY area_pop
LIMIT 10
)
SELECT *
FROM smallest_countries
ORDER BY gnp DESC;
--Bermuda
WITH smallest_countries AS(
SELECT code, name, surfacearea, population, gnp, gnp / population AS per_cap_gnp, surfacearea * population AS area_pop
FROM country
WHERE population !=0 AND gnp !=0
ORDER BY area_pop
LIMIT 10
)
SELECT *
FROM smallest_countries
ORDER BY per_cap_gnp DESC;
--Bermuda
-- Biggest Countries, top gnp
WITH biggest_countries AS(
SELECT code, name, surfacearea, population, gnp, gnp / population AS per_cap_gnp, surfacearea * population AS area_pop
FROM country
WHERE population !=0 AND gnp !=0
ORDER BY area_pop DESC
LIMIT 10
)
SELECT *
FROM biggest_countries
ORDER BY gnp DESC;
-- USA
-- biggest per cap GNP
WITH biggest_countries AS(
SELECT code, name, surfacearea, population, gnp, gnp / population AS per_cap_gnp, surfacearea * population AS area_pop
FROM country
WHERE population !=0 AND gnp !=0
ORDER BY area_pop DESC
LIMIT 10
)
SELECT *
FROM biggest_countries
ORDER BY per_cap_gnp DESC;
-- USA
-- SUM of SURFACE AREA of BIGGEST COUNTRIES IN THE WORLD
WITH biggest_countries AS(
SELECT code, name, surfacearea, population, gnp, gnp / population AS per_cap_gnp, surfacearea * population AS area_pop
FROM country
WHERE population !=0 AND gnp !=0
ORDER BY area_pop DESC
LIMIT 10
)
SELECT SUM(biggest_countries.surfacearea)
FROM biggest_countries;
'7.02172e+07'
-- SUM of SURFACE AREA of SMALLEST COUNTRIES IN THE WORLD
WITH smallest_countries AS(
SELECT code, name, surfacearea, population, gnp, gnp / population AS per_cap_gnp, surfacearea * population AS area_pop
FROM country
WHERE population !=0 AND gnp !=0
ORDER BY area_pop ASC
LIMIT 10
)
SELECT SUM(smallest_countries.surfacearea)
FROM smallest_countries;
'517.9'
-- Size of CONTINENTS IN AREA AND POP
SELECT continent, SUM(surfacearea) as area, SUM(population) as pop
FROM country
GROUP BY continent;
--
-- 'Europe','2.30491e+07','730074600'
-- 'Oceania','8.56429e+06','30401150'
-- 'Asia','3.1881e+07','3705025700'
-- 'North America','2.42145e+07','482993000'
-- 'Africa','3.02504e+07','784475000'
-- 'Antarctica','1.31321e+07','0'
-- 'South America','1.78649e+07','345780000'
-- Region w/ Highest Avg GNP
SELECT region, AVG(gnp) as avg_gnp
FROM country
GROUP BY region
ORDER BY avg_gnp DESC
-- North America 1822378
-- Most Influential Head of State measured by Pop
SELECT name, headofstate, population
FROM country
ORDER BY population DESC;
-- 'China','Jiang Zemin',1277558000
-- Most influential head of state measured by surfacearea
SELECT name, headofstate, surfacearea
FROM country
WHERE headofstate != ''
ORDER BY surfacearea DESC;
-- 'Russian Federation','Vladimir Putin','1.70754e+07'
-- Most common forms of GOVT
SELECT governmentform, COUNT(governmentform) AS gov_count
FROM country
GROUP BY governmentform
ORDER BY gov_count DESC;
-- Republic
-- What are the forms of government for the top ten countries by surface area?
WITH biggest_countries AS(
SELECT code, name, surfacearea, population, gnp, governmentform
FROM country
WHERE population !=0 AND gnp !=0
ORDER BY surfacearea DESC
LIMIT 10
)
SELECT *
FROM biggest_countries
ORDER BY surfacearea DESC;
-- What are the forms of government for the top ten richest nations? (technically most productive)
SELECT code, name, surfacearea, population, gnp, governmentform
FROM country
WHERE population !=0 AND gnp !=0
ORDER BY gnp DESC
LIMIT 10
-- 'USA','United States','9.36352e+06',278357000,'8510700.00','Federal Republic'
-- 'JPN','Japan','377829',126714000,'3787042.00','Constitutional Monarchy'
-- 'DEU','Germany','357022',82164700,'2133367.00','Federal Republic'
-- 'FRA','France','551500',59225700,'1424285.00','Republic'
-- 'GBR','United Kingdom','242900',59623400,'1378330.00','Constitutional Monarchy'
-- 'ITA','Italy','301316',57680000,'1161755.00','Republic'
-- 'CHN','China','9.5729e+06',1277558000,'982268.00','People'sRepublic'
-- 'BRA','Brazil','8.5474e+06',170115000,'776739.00','Federal Republic'
-- 'CAN','Canada','9.97061e+06',31147000,'598862.00','Constitutional Monarchy, Federation'
-- 'ESP','Spain','505992',39441700,'553233.00','Constitutional Monarchy'
-- What are the forms of government for the top ten richest per capita nations? (technically most productive)
-- What is the 3rd most common language spoken?
SELECT
c.code AS code,
c.name AS country,
c.population as population,
cl.language as language,
cl.percentage as percentage,
(percentage/100) * population AS pop_speak_lang,
FROM
country c JOIN
countrylanguage cl ON c.code = cl.countrycode
ORDER BY
pop_speak_lang DESC
SELECT
c.code AS code
FROM
country c JOIN
city ct ON c.code = ct.countrycode
WHERE
code = "CHL"
-- How many cities are in Chile?
SELECT
count(countrycode) as ct
FROM
city
WHERE
countrycode = 'CHL'
-- What is the total population in China?
SELECT
code, name, population
FROM
country
WHERE
name = 'China';
-- How many countries are in North America?
SELECT continent, count(country.code) AS number_of_countries
FROM country
WHERE continent = 'North America'
GROUP BY continent
-- Which countries gained their independence before 1963?
SELECT name, indepyear
FROM COUNTRY
WHERE indepyear < 1963
ORDER BY indepyear DESC;
-- What is the total population of all continents?
WITH country_pops AS(
SELECT continent, sum(country.population) AS pop
FROM country
GROUP BY continent
)
SELECT sum(country_pops.pop) as country_sum
FROM country_pops
-- What is the average life expectancy for all continents?
WITH country_exp AS(
SELECT continent, avg(country.lifeexpectancy) AS life_exp
FROM country
GROUP BY continent
ORDER BY life_exp DESC
)
SELECT avg(country_exp.life_exp)
FROM country_exp
-- Which countries have the letter z in the name? How many?
SELECT name
FROM country
WHERE name like '%z%'
'Azerbaijan'
'Belize'
'Bosnia and Herzegovina'
'Brazil'
'Kazakstan'
'Kyrgyzstan'
'Mozambique'
'Swaziland'
'Switzerland'
'Tanzania'
'Czech Republic'
'Uzbekistan'
'Venezuela'
-- What is the age of Jamaica?
SELECT name, indepyear, (2017 - indepyear) as age
FROM country
WHERE name LIKE 'Jamaica'
-- Are there any countries without an official language? Hint: WHERE ... NOT IN ( SELECT ... FROM ... WHERE ...)
SELECT *
FROM countrylanguage
WHERE isofficial = false