sqlzoo
name
continent
area
population
gdp
Afghanistan
Asia
652230
25500100
20343000000
Albania
Europe
28748
2831741
12960000000
Algeria
Africa
2381741
37100000
188681000000
Andorra
Europe
468
78115
3712000000
Angola
Africa
1246700
20609294
100990000000
...
Rounding
Show thename
andpopulation
in millions and the GDP in billions for the countries of thecontinent
'South America'. Use theROUNDfunction to show the values to two decimal places.
For South America show population in millions and GDP in billions both to 2 decimal places.
Note: ROUND(X
,D
) rounds the argument X
_to D
_decimal places.
Trillion dollar economies
Show thename
and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.
Show per-capita GDP for the trillion dollar countries to the nearest $1000.
Note: ROUND(X)
rounds the argument X
to the nearest number. (四舍五入)
Matching name and capital
Show the name and the capital where the first letters of each match. Don't include countries where the name and the capital are the same word.
Note: LEFT(string, num)
return the leftmost num
characters in string
.
All the vowels
Find the country that has all the vowels and no spaces in its name.
Percentages of Germany
Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.
Note:
To use % sign, need to use CONCAT() function
To get the right percentage number, need to use ROUND() function.
Remember *100 when you compute the percentage number, to take in account of the following % sign.
Bigger than every country in Europe
Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)
Largest in each continent
Find the largest country (by area) in each continent, show the continent, the name and the area:
The difficulty of this question is how to show name. Using GROUP BY, we can easily show continent and MAX(area). But we can't show name in GROUP BY clauses.
Solution 1:
Use a subquery to list the max area in each continent, then select those matches.
Solution 2:
Do a left join between the GROUP BY table and the original table, and then return output we want.
Solution 3:
Use correlated subqueries
Note: need area > 0
condition because area
may be NULL
. If it's NULL
, the corresponding continent won't be returned.
First country of each continent (alphabetically)
Solution 1: List each continent and the name of the country that comes first alphabetically.
Solution 2: use correlated subquery
Continents with all small population countries
Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show name, continent and population.
Countries larger than all their neighbours
Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.
Last updated