-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase-intro-olena-goran.sql
170 lines (137 loc) · 4.83 KB
/
database-intro-olena-goran.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
-- What is the population of the US? (HINT: 278357000)
SELECT name, population
FROM country
WHERE name = 'United States'
-- What is the area of the US? (HINT: 9.36352e+06)
SELECT name, surfacearea
FROM country
WHERE name = 'United States'
-- Which countries gained their independence before 1963?
SELECT name, indepyear
FROM country
WHERE indepyear < 1963
-- List the countries in Africa that have a population smaller than 30,000,000 and a life expectancy of more than 45? (HINT: 37 entries)
SELECT name, population, lifeexpectancy, continent
FROM country
WHERE continent = 'Africa'
AND population < 3e+7
AND lifeexpectancy > 45
-- Which countries are something like a republic? (HINT: Are there 122 or 143?)
SELECT name, governmentform
FROM country
WHERE governmentform IN ('Republic')
-- Which countries are some kind of republic and achieved independence after 1945? (HINT: 92 entries)
SELECT name, governmentform,indepyear
FROM country
WHERE indepyear > 1945
AND governmentform LIKE '%Republic'
-- Which countries achieved independence after 1945 and are not some kind of republic? (HINT: 27 entries)
SELECT name, governmentform,indepyear
FROM country
WHERE indepyear > 1945
AND NOT governmentform LIKE ('%Republic%')
----------------------------------------------------------------------------
-- ORDER BY
-- I do not use ASC because The ORDER BY keyword sorts the records in ascending order by default.
-- Which fifteen countries have the lowest life expectancy? (HINT: starts with Zambia, ends with Sierra Leonne)
SELECT name, lifeexpectancy
FROM country
ORDER BY lifeexpectancy
LIMIT 15
-- Which fifteen countries have the highest life expectancy? (HINT: starts with Andorra, ends with Spain)
SELECT name, lifeexpectancy
FROM country
WHERE lifeexpectancy IS NOT NULL
ORDER BY lifeexpectancy DESC
LIMIT 15
-- Which five countries have the lowest population density (density = population / surfacearea)? (HINT: starts with Greenland)
SELECT *
FROM country
WHERE population != 0
ORDER BY population / surfacearea
LIMIT 5
-- Which countries have the highest population density?(HINT: starts with Macao)
-- Result - I got 239 countries. Starts with Macao.
SELECT name
FROM country
ORDER BY population / surfacearea DESC
-- Which is the smallest country by area? (HINT: .4)
-- I do not use ASC because The ORDER BY keyword sorts the records in ascending order by default.
SELECT *
FROM country
ORDER BY surfacearea
-- Which is the smallest country by population? (HINT: 50)?
-- I do not use ASC because The ORDER BY keyword sorts the records in ascending order by default.
SELECT *
FROM country
WHERE population != 0
ORDER BY population
-- Which is the biggest country by area? (HINT: 1.70754e+07)
SELECT *
FROM country
ORDER BY surfacearea DESC
-- Which is the biggest country by population? (HINT: 1277558000)
SELECT *
FROM country
ORDER BY population DESC
-- Who is the most influential head of state measured by population? (HINT: Jiang Zemin)
SELECT headofstate
FROM country
ORDER BY population DESC
--------------------------------------------------------------------------
-- Subqueries: WITH
-- Of the countries with the top 10 gnp, which has the smallest population? (HINT: Canada)
WITH top_gnp AS (
SELECT name, population, gnp
FROM country
WHERE population > 0
ORDER BY gnp DESC
LIMIT 10
)
SELECT name, population, gnp
FROM top_gnp
ORDER BY population ASC
LIMIT 10
-- Of the 10 least populated countries with permament residents (a non-zero population), which has the largest surfacearea? (HINT: Svalbard and Jan Mayen)
WITH least_populated AS (
SELECT name, population, surfacearea
FROM country
WHERE population > 0
ORDER BY population
LIMIT 10
)
SELECT name, population, surfacearea
FROM least_populated
ORDER BY surfacearea DESC
LIMIT 1
------------------------------------------------------------------------------
-- Aggregate Functions: GROUP BY
-- Which region has the highest average gnp? (HINT: North America)
SELECT region, AVG(gnp)
FROM country
GROUP BY region, country.gnp
ORDER BY country.gnp DESC
-- Who is the most influential head of state measured by surface area? (HINT: Elisabeth II)
--This one was the most difficult to me. Every tyme I got Putin 🤪 Finally ELISABETH II 🥳
SELECT headofstate, SUM(surfacearea)
FROM country
GROUP BY headofstate
ORDER BY SUM(surfacearea) DESC
-- What is the average life expectancy for all continents?
SELECT continent, AVG(lifeexpectancy)
FROM country
GROUP BY continent
-- What are the most common forms of government? (HINT: use count(*))
SELECT governmentform, COUNT(governmentform)
FROM country
GROUP BY governmentform
ORDER BY COUNT(governmentform) DESC;
-- How many countries are in North America?
SELECT region, COUNT(name)
FROM country
WHERE region='North America'
GROUP BY region
-- What is the total population of all continents?
SELECT continent, SUM(population)
FROM country
GROUP BY continent