|
Tutorial One: Answers |
Note that these answers are not unique. There are many alternatives.
SELECT * FROM cia
| name | region | area | population | gdp |
|---|---|---|---|---|
| Afghanistan | Asia | 647500 | 21251821 | 18100000000 |
| Albania | Ethnic Groups in Eastern Europe, Europe | 28750 | 3413904 | 3800000000 |
| Algeria | Africa | 2381740 | 28539321 | 97100000000 |
| ... | ||||
Give the names and the GDP for the countries with populations of at least 200 million (200000000)
SELECT name, gdp FROM cia WHERE population>200000000;
| name | gdp |
|---|---|
| China | 2978800000000 |
| India | 1253900000000 |
| Indonesia | 619400000000 |
| United States | 6738400000000 |
Give the names and the per capita GDP for the countries with populations of at least 200 million
SELECT name, gdp/population FROM cia WHERE population>200000000;
| name | gdp / population |
|---|---|
| China | 2476 |
| India | 1339 |
| Indonesia | 3042 |
| United States | 25542 |
Show the names and populations in millions for the countries of 'South America'
SELECT name,population/1000000 FROM cia WHERE region='South America';
| name | population / 1000000 |
|---|---|
| Argentina | 34.2927 |
| Bolivia | 7.8962 |
| Brazil | 160.7375 |
| Chile | 14.1612 |
| Colombia | 36.2002 |
| Ecuador | 10.891 |
| Falkland Islands (Islas Malvinas) | 0.0023 |
| French Guiana | 0.1453 |
| Guyana | 0.7238 |
| Paraguay | 5.3582 |
| Peru | 24.0874 |
| Suriname | 0.4295 |
| Uruguay | 3.2227 |
| Venezuela | 21.0048 |
Show the populations of 'France', 'Germany', 'Italy'
SELECT name,population FROM cia
WHERE name in ('France', 'Germany', 'Italy');
| name | population |
|---|---|
| France | 58109160 |
| Germany | 81337541 |
| Italy | 58261971 |
Which countries have names including the word 'United'?
SELECT name FROM cia WHERE name LIKE 'United%';
| name |
|---|
| United Arab Emirates |
| United Kingdom |
| United States |
Which countries have populations larger than 'United States'
SELECT name FROM cia
WHERE population >
(SELECT population FROM cia WHERE name='United States');
| name |
|---|
| China |
| India |
List the countries of the regions containing 'Ukraine', 'Hungary'
We note that the query
SELECT region FROM cia
WHERE name IN ('Ukraine', 'Hungary')
returns a list of two regions:
| region |
|---|
| Commonwealth of Independent States - European States |
| Ethnic Groups in Eastern Europe, Europe |
This table result can be used with an IN clause:
SELECT name FROM cia
WHERE region IN
(SELECT region FROM cia WHERE name IN ('Ukraine', 'Hungary'));
| name |
|---|
| Albania |
| Armenia |
| Azerbaijan |
| Belarus |
| Bosnia and Herzegovina |
| Bulgaria |
| Croatia |
| Czech Republic |
| Hungary |
| Macedonia, The Former Yugoslav Republic of |
| Moldova |
| Poland |
| Romania |
| Serbia and Montenegro |
| Slovakia |
| Slovenia |
| Ukraine |
Show the European countries with a per capita GDP greater than 'United Kingdom'
SELECT name FROM cia WHERE region = 'Europe' AND gdp/population > (SELECT gdp/population FROM cia WHERE name='United Kingdom');
| name |
|---|
| Belgium |
| Denmark |
| France |
| Liechtenstein |
| Luxembourg |
| Norway |
| Sweden |
| Switzerland |
Depressing isn't it, but where is Germany?
Show the total population of the world.
SELECT SUM(population) FROM cia;
| SUM OF population |
|---|
| 5729533973 |
List all the regions.
SELECT DISTINCT region FROM cia;
| region |
|---|
| Africa |
| Antarctic Region |
| Arctic Region |
| Asia |
| Central America and the Caribbean |
| Commonwealth of Independent States - Central Asian States |
| Commonwealth of Independent States - European States |
| Ethnic Groups in Eastern Europe, Europe |
| Europe |
| Middle East |
| North America |
| Oceania |
| South America |
| Southeast Asia |
| World |
List the countries which have a GDP greater than the whole of Africa
SELECT name FROM cia WHERE gdp > (SELECT SUM(gdp) FROM cia WHERE region='Africa');
| name |
|---|
| China |
| Germany |
| India |
| Japan |
| United States |
For each region show the number of countries of at least 10 million.
SELECT region, COUNT(name) FROM cia WHERE population > 10000000 GROUP BY region;
| region | COUNT OF name |
|---|---|
| Africa | 19 |
| Asia | 11 |
| Central America and the Caribbean | 2 |
| Commonwealth of Independent States - Central Asian States | 2 |
| Commonwealth of Independent States - European States | 2 |
| Ethnic Groups in Eastern Europe, Europe | 5 |
| Europe | 9 |
| Middle East | 6 |
| North America | 3 |
| Oceania | 1 |
| South America | 7 |
| Southeast Asia | 8 |
List the regions with total populations of at least 100 million.
SELECT region, SUM(population) FROM cia GROUP BY region HAVING SUM(population)>100000000;
Note SQL Executor does not support HAVING.
| region | SUM OF population |
|---|---|
| Africa | 715369983 |
| Asia | 2809426876 |
| Ethnic Groups in Eastern Europe, Europe | 123057096 |
| Europe | 393708823 |
| Middle East | 227395296 |
| North America | 386302811 |
| South America | 319152866 |
| Southeast Asia | 522458680 |