%%capture
%load_ext sql
%sql sqlite:///factbook.db
'Connected: None@factbook.db'
%%sql
SELECT *
FROM facts
LIMIT 5;
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate |
---|---|---|---|---|---|---|---|---|---|---|
1 | af | Afghanistan | 652230 | 652230 | 0 | 32564342 | 2.32 | 38.57 | 13.89 | 1.51 |
2 | al | Albania | 28748 | 27398 | 1350 | 3029278 | 0.3 | 12.92 | 6.58 | 3.3 |
3 | ag | Algeria | 2381741 | 2381741 | 0 | 39542166 | 1.84 | 23.67 | 4.31 | 0.92 |
4 | an | Andorra | 468 | 468 | 0 | 85580 | 0.12 | 8.13 | 6.96 | 0.0 |
5 | ao | Angola | 1246700 | 1246700 | 0 | 19625353 | 2.78 | 38.78 | 11.49 | 0.46 |
Below are the description of some of the columns in the table(facts):
%%sql
SELECT MIN(population) AS min_population,
MAX(population) AS max_population,
MIN(population_growth) AS min_pop_growth,
MAX(population_growth) AS max_pop_growth
FROM facts;
Done.
min_population | max_population | min_pop_growth | max_pop_growth |
---|---|---|---|
0 | 7256490011 | 0.0 | 4.02 |
From the result of our summary statistics we can see that there is a country with Population 0, Another one has a population of over 7.2 billion. In the next cell, we are going to use subqueries to zoom in on these countries with the above populations.
%%sql
SELECT *
FROM facts
WHERE population = (SELECT MIN(Population)
FROM facts);
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate |
---|---|---|---|---|---|---|---|---|---|---|
250 | ay | Antarctica | None | 280000 | None | 0 | None | None | None | None |
From our output we can see that Antartica is the country with zero population. If we want to know why the Population is 0, we can find out from here.
%%sql
SELECT *
FROM facts
WHERE population = (SELECT MAX(Population)
FROM facts);
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate |
---|---|---|---|---|---|---|---|---|---|---|
261 | xx | World | None | None | None | 7256490011 | 1.08 | 18.6 | 7.8 | None |
From our output we can see that the name returned is the Population for the whole world, This explains the ambiguous population. In our next code cell we will recompute our summary and remove the 'world' country, so that we can have an accurate result.
%%sql
SELECT MIN(population) AS min_population,
MAX(population) AS max_population,
MIN(population_growth) AS min_pop_growth,
MAX(population_growth) AS max_pop_growth
FROM facts
WHERE name <> 'World';
Done.
min_population | max_population | min_pop_growth | max_pop_growth |
---|---|---|---|
0 | 1367485388 | 0.0 | 4.02 |
There is a country that has a population of approximately 1.4 Billion
%%sql
SELECT *
FROM facts
WHERE population = (SELECT MAX(Population)
FROM facts
WHERE name <> 'World');
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate |
---|---|---|---|---|---|---|---|---|---|---|
37 | ch | China | 9596960 | 9326410 | 270550 | 1367485388 | 0.45 | 12.49 | 7.53 | 0.44 |
From our output above, we discovered that China is the country with the highest population of about 1.4 Billion.
%%sql
SELECT AVG(population) AS avg_pop, AVG(area) AS avg_area
FROM facts
WHERE name <> 'World';
Done.
avg_pop | avg_area |
---|---|
32242666.56846473 | 555093.546184739 |
From our output the average population is around 32 million whereas the average area is approximately 555 thousand Kilometer square.
To finish, we'll build on the query we wrote for the previous screen to find countries that are densely populated. We'll identify countries that have:
Above average values for population. Below average values for area.
%%sql
SELECT *
FROM facts
WHERE population > (SELECT AVG(population) FROM facts
WHERE name <> 'World')
AND area < (SELECT AVG(area) FROM facts
WHERE name <> 'World');
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate |
---|---|---|---|---|---|---|---|---|---|---|
14 | bg | Bangladesh | 148460 | 130170 | 18290 | 168957745 | 1.6 | 21.14 | 5.61 | 0.46 |
65 | gm | Germany | 357022 | 348672 | 8350 | 80854408 | 0.17 | 8.47 | 11.42 | 1.24 |
80 | iz | Iraq | 438317 | 437367 | 950 | 37056169 | 2.93 | 31.45 | 3.77 | 1.62 |
83 | it | Italy | 301340 | 294140 | 7200 | 61855120 | 0.27 | 8.74 | 10.19 | 4.1 |
85 | ja | Japan | 377915 | 364485 | 13430 | 126919659 | 0.16 | 7.93 | 9.51 | 0.0 |
91 | ks | Korea, South | 99720 | 96920 | 2800 | 49115196 | 0.14 | 8.19 | 6.75 | 0.0 |
120 | mo | Morocco | 446550 | 446300 | 250 | 33322699 | 1.0 | 18.2 | 4.81 | 3.36 |
138 | rp | Philippines | 300000 | 298170 | 1830 | 100998376 | 1.61 | 24.27 | 6.11 | 2.09 |
139 | pl | Poland | 312685 | 304255 | 8430 | 38562189 | 0.09 | 9.74 | 10.19 | 0.46 |
163 | sp | Spain | 505370 | 498980 | 6390 | 48146134 | 0.89 | 9.64 | 9.04 | 8.31 |
173 | th | Thailand | 513120 | 510890 | 2230 | 67976405 | 0.34 | 11.19 | 7.8 | 0.0 |
182 | ug | Uganda | 241038 | 197100 | 43938 | 37101745 | 3.24 | 43.79 | 10.69 | 0.74 |
185 | uk | United Kingdom | 243610 | 241930 | 1680 | 64088222 | 0.54 | 12.17 | 9.35 | 2.54 |
192 | vm | Vietnam | 331210 | 310070 | 21140 | 94348835 | 0.97 | 15.96 | 5.93 | 0.3 |
In conclusion, if we compare our result with the real data of some of these countries, we will find the result to be true.
%%sql
SELECT *
FROM facts
WHERE population_growth = (SELECT MAX(Population_growth)
FROM facts
WHERE name <> 'World');
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate |
---|---|---|---|---|---|---|---|---|---|---|
162 | od | South Sudan | 644329 | None | None | 12042910 | 4.02 | 36.91 | 8.18 | 11.47 |
From our ouput South sudan has the highest growth rate of 4.02
%%sql
SELECT *, CAST(area_water AS FLOAT) / area_land AS ratio_water_land
FROM facts
WHERE ratio_water_land = (SELECT MAX(CAST(area_water AS FLOAT) / area_land)
FROM facts
WHERE name <> 'World');
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate | ratio_water_land |
---|---|---|---|---|---|---|---|---|---|---|---|
228 | io | British Indian Ocean Territory | 54400 | 60 | 54340 | None | None | None | None | None | 905.6666666666666 |
%%sql
SELECT *
FROM facts
WHERE area_water > area_land
AND name <> 'World';
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate |
---|---|---|---|---|---|---|---|---|---|---|
228 | io | British Indian Ocean Territory | 54400 | 60 | 54340 | None | None | None | None | None |
247 | vq | Virgin Islands | 1910 | 346 | 1564 | 103574 | 0.59 | 10.31 | 8.54 | 7.67 |
Our output shows that British Indian Ocean Territory has more water than Land and higest ratio of water to land.
%%sql
SELECT *
FROM facts
WHERE migration_rate = (SELECT MAX(migration_rate) FROM facts
WHERE name <> 'World')
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate |
---|---|---|---|---|---|---|---|---|---|---|
141 | qa | Qatar | 11586 | 11586 | 0 | 2194817 | 3.07 | 9.84 | 1.53 | 22.39 |
Qatar from our output has the highest migration rate
%%sql
SELECT *
FROM facts
WHERE population * population_growth/100 = (SELECT MAX(population * population_growth/100)
FROM facts
WHERE name <> 'World');
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate |
---|---|---|---|---|---|---|---|---|---|---|
77 | in | India | 3287263 | 2973193 | 314070 | 1251695584 | 1.22 | 19.55 | 7.32 | 0.04 |
India has the highest probablity of adding to their populatio next year based on the ratio between their population and Population growth.
%%sql
SELECT *
FROM facts
WHERE death_rate > birth_rate
AND name <> 'World';
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate |
---|---|---|---|---|---|---|---|---|---|---|
10 | au | Austria | 83871 | 82445 | 1426 | 8665550 | 0.55 | 9.41 | 9.42 | 5.56 |
16 | bo | Belarus | 207600 | 202900 | 4700 | 9589689 | 0.2 | 10.7 | 13.36 | 0.7 |
22 | bk | Bosnia and Herzegovina | 51197 | 51187 | 10 | 3867055 | 0.13 | 8.87 | 9.75 | 0.38 |
26 | bu | Bulgaria | 110879 | 108489 | 2390 | 7186893 | 0.58 | 8.92 | 14.44 | 0.29 |
44 | hr | Croatia | 56594 | 55974 | 620 | 4464844 | 0.13 | 9.45 | 12.18 | 1.39 |
47 | ez | Czech Republic | 78867 | 77247 | 1620 | 10644842 | 0.16 | 9.63 | 10.34 | 2.33 |
57 | en | Estonia | 45228 | 42388 | 2840 | 1265420 | 0.55 | 10.51 | 12.4 | 3.6 |
65 | gm | Germany | 357022 | 348672 | 8350 | 80854408 | 0.17 | 8.47 | 11.42 | 1.24 |
67 | gr | Greece | 131957 | 130647 | 1310 | 10775643 | 0.01 | 8.66 | 11.09 | 2.32 |
75 | hu | Hungary | 93028 | 89608 | 3420 | 9897541 | 0.22 | 9.16 | 12.73 | 1.33 |
83 | it | Italy | 301340 | 294140 | 7200 | 61855120 | 0.27 | 8.74 | 10.19 | 4.1 |
85 | ja | Japan | 377915 | 364485 | 13430 | 126919659 | 0.16 | 7.93 | 9.51 | 0.0 |
96 | lg | Latvia | 64589 | 62249 | 2340 | 1986705 | 1.06 | 10.0 | 14.31 | 6.26 |
102 | lh | Lithuania | 65300 | 62680 | 2620 | 2884433 | 1.04 | 10.1 | 14.27 | 6.27 |
116 | md | Moldova | 33851 | 32891 | 960 | 3546847 | 1.03 | 12.0 | 12.59 | 9.67 |
117 | mn | Monaco | 2 | 2 | 0 | 30535 | 0.12 | 6.65 | 9.24 | 3.83 |
139 | pl | Poland | 312685 | 304255 | 8430 | 38562189 | 0.09 | 9.74 | 10.19 | 0.46 |
140 | po | Portugal | 92090 | 91470 | 620 | 10825309 | 0.09 | 9.27 | 11.02 | 2.67 |
142 | ro | Romania | 238391 | 229891 | 8500 | 21666350 | 0.3 | 9.14 | 11.9 | 0.24 |
143 | rs | Russia | 17098242 | 16377742 | 720500 | 142423773 | 0.04 | 11.6 | 13.69 | 1.69 |
153 | ri | Serbia | 77474 | 77474 | 0 | 7176794 | 0.46 | 9.08 | 13.66 | 0.0 |
158 | si | Slovenia | 20273 | 20151 | 122 | 1983412 | 0.26 | 8.42 | 11.37 | 0.37 |
183 | up | Ukraine | 603550 | 579330 | 24220 | 44429471 | 0.6 | 10.72 | 14.46 | 2.25 |
214 | sb | Saint Pierre and Miquelon | 242 | 242 | 0 | 5657 | 1.08 | 7.42 | 9.72 | 8.49 |
%%sql
SELECT *
FROM facts
WHERE population / area= (SELECT MAX(population /area)
FROM facts
WHERE name <> 'World');
Done.
id | code | name | area | area_land | area_water | population | population_growth | birth_rate | death_rate | migration_rate |
---|---|---|---|---|---|---|---|---|---|---|
205 | mc | Macau | 28 | 28 | 0 | 592731 | 0.8 | 8.88 | 4.22 | 3.37 |
Macau has the highest populatio/area ratio and the area of land in square kilometer is so small posibly compared to other countries.