SELECT From WORLD Tutorial
SELECT From WORLD Tutorial
In this tutorial you will use the SELECT command on the table world:
Contents
Introduction
Large Countries
Per capita GDP
South America In millions
France, Germany, Italy
United
Two ways to be big
One or the other (but not both)
Rounding
Trillion dollar economies
Name and capital have the same length
Matching name and capital
All the vowels
What Next
Introduction
1.
Read the notes about this table. Observe the result of running this SQL command to show the name, continent and
population of all countries.
SELECT name, continent, population FROM world
result
Large Countries
2.
How to use WHERE to filter records. Show the name for the countries that have a population of at least 200 million.
200 million is 200000000, there are eight zeros.
result
Per capita GDP
3.
Give the name and the per capita GDP for those countries with a population of at least 200 million.
sfwQ3
Correct answer
country per_capita_gdp
Brazil 11115.2648
China 6121.7106
India 1504.7931
Indonesia 3482.0205
United States 51032.2945
4.
Show the name and population in millions for the countries of the continent 'South America'. Divide the population
by 1000000 to get population in millions.
Correct answer
country pop_millions
Argentina 42.6695
Bolivia 10.0273
Brazil 202.7940
Chile 17.7730
Colombia 47.6620
Ecuador 15.7742
Guyana 0.7849
Paraguay 6.7834
Peru 30.4751
5.
Show the name and population for France, Germany, Italy
result
United
6.
Show the countries which have a name that includes the word 'United'
result
7.
Two ways to be big: A country is big if it has an area of more than 3 million sq km or it has a population of more than
250 million.
Show the countries that are big by area or big by population. Show name, population and area.
Submit SQL Restore default
result
8.
Exclusive OR (XOR). Show the countries that are big by area or big by population but not both. Show
name, population and area.
Correct answer
name population area
Australia 23545500 7692024
Brazil 202794000 8515767
Canada 35427524 9984670
Indonesia 252164800 1904569
Russia 146000000 17125242
Rounding
9.
Show the name and population in millions and the GDP in billions for the countries of the continent 'South
America'. Use the ROUND function to show the values to two decimal places.
For South America show population in millions and GDP in billions both to 2
decimal places.
Millions and billions
Divide by 1000000 (6 zeros) for millions. Divide by 1000000000 (9 zeros) for billions.
SELECT name, ROUND(population/1000000,2), ROUND(gdp/1000000000, 2)
FROM world
WHERE continent = 'South America'
Correct answer
name ROUND(populat.. ROUND(gdp/100..
Argentina 42.67 477.03
Bolivia 10.03 27.04
Brazil 202.79 2254.11
Chile 17.77 268.31
Colombia 47.66 369.81
Ecuador 15.77 87.50
Guyana 0.78 2.85
Paraguay 6.78 25.94
Peru 30.48 204.68
S i t Vi t d th G di 0 11 0 69
10.
Show the name 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.
SELECT name, ROUND(gdp/population, -3)
FROM world
WHERE gdp > 1000000000000
Correct answer
name ROUND(gdp/pop..
Australia 66000
Brazil 11000
Canada 45000
China 6000
France 40000
Germany 42000
India 2000
Italy 33000
Japan 47000
M i 10000
11.
Greece has capital Athens.
Show the name and capital where the name and the capital have the same number of characters.
You can use the LENGTH function to find the number of characters in a string
12.
The capital of Sweden is Stockholm. Both words start with the letter 'S'.
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.
You can use the function LEFT to isolate the first character.
You can use <> as the NOT EQUALS operator.
SELECT name, capital
FROM world
WHERE LEFT(name,1) = LEFT(capital,1) and name<>capital
Correct answer
name capital
Algeria Algiers
Andorra Andorra la Vella
Barbados Bridgetown
Belize Belmopan
Brazil Brasília
Brunei Bandar Seri Begawan
Burundi Bujumbura
Guatemala Guatemala City
Guyana Georgetown
13.
Equatorial Guinea and Dominican Republic have all of the vowels (a e i o u) in the name. They don't count
because they have more than one word in the name.
Find the country that has all the vowels and no spaces in its name.
You can use the phrase name NOT LIKE '%a%' to exclude characters from your results.
The query shown misses countries like Bahamas and Belarus because they contain at least one 'a'
SELECT name FROM world
WHERE name LIKE '%a%' AND
name LIKE '%e%' AND
name LIKE '%i%' AND
name LIKE '%o%' AND
name LIKE '%u%' AND
name NOT LIKE '% %'
Correct answer
name
Mozambique
What Next
Click here to play the SELECT card game (http://sqlzoo.net/40289347/sql3/)
BBC QUIZ
You can to continue practising the the same techniques and gain more experience of the basic skills on the Nobel
table. The WHERE statement using the nobel table.
You can learn about nested statements, these are instructive and entertaining, but not essential for beginners.
Nested SELECT statements using the world table.