Difference between revisions of "SELECT from WORLD Tutorial"
(→Per capita GDP) |
|||
(43 intermediate revisions by 2 users not shown) | |||
Line 15: | Line 15: | ||
</div> | </div> | ||
− | + | ||
In this tutorial you will use the SELECT command on the table <code>world</code>: | In this tutorial you will use the SELECT command on the table <code>world</code>: | ||
<div class='extra_space' style='width:1em; height:2.5em;'></div> | <div class='extra_space' style='width:1em; height:2.5em;'></div> | ||
+ | ==Introduction== | ||
<div class='qu'> | <div class='qu'> | ||
[[Read_the_notes_about_this_table. |Read the notes about this table.]] Observe the result of running this SQL command to show the name, continent and population of all countries. | [[Read_the_notes_about_this_table. |Read the notes about this table.]] Observe the result of running this SQL command to show the name, continent and population of all countries. | ||
Line 29: | Line 30: | ||
SELECT name, continent, population FROM world | SELECT name, continent, population FROM world | ||
</source> | </source> | ||
− | |||
</div> | </div> | ||
+ | ==Large Countries== | ||
<div class='qu'> | <div class='qu'> | ||
[[WHERE_filters |How to use WHERE to filter records.]] | [[WHERE_filters |How to use WHERE to filter records.]] | ||
Line 40: | Line 41: | ||
WHERE population = 64105700 | WHERE population = 64105700 | ||
</source> | </source> | ||
− | |||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
SELECT name FROM world | SELECT name FROM world | ||
Line 47: | Line 47: | ||
</div> | </div> | ||
− | + | ==Per capita GDP== | |
<div class='qu'> | <div class='qu'> | ||
Give the <code>name</code> and the '''per capita GDP''' for those countries with a <code>population</code> of at least 200 million. | Give the <code>name</code> and the '''per capita GDP''' for those countries with a <code>population</code> of at least 200 million. | ||
Line 55: | Line 55: | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
</source> | </source> | ||
− | |||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
SELECT name, gdp/population FROM world | SELECT name, gdp/population FROM world | ||
WHERE population > 200000000 | WHERE population > 200000000 | ||
</source> | </source> | ||
+ | {{#ev:youtube|5Md75Wfpocs}} | ||
</div> | </div> | ||
+ | ==South America In millions== | ||
<div class='qu'> | <div class='qu'> | ||
Show the <code>name</code> and <code>population</code> in millions for the countries of the <code>continent</code> 'South America'. | Show the <code>name</code> and <code>population</code> in millions for the countries of the <code>continent</code> 'South America'. | ||
Line 67: | Line 68: | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
</source> | </source> | ||
− | |||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
SELECT name, population/1000000 FROM world | SELECT name, population/1000000 FROM world | ||
Line 74: | Line 74: | ||
</div> | </div> | ||
+ | ==France, Germany, Italy== | ||
<div class='qu'> | <div class='qu'> | ||
Show the <code>name</code> and <code>population</code> for France, Germany, Italy | Show the <code>name</code> and <code>population</code> for France, Germany, Italy | ||
Line 85: | Line 86: | ||
</div> | </div> | ||
+ | ==United== | ||
<div class='qu'> | <div class='qu'> | ||
Show the countries which have a <code>name</code> that includes the word 'United' | Show the countries which have a <code>name</code> that includes the word 'United' | ||
Line 97: | Line 99: | ||
<div> | <div> | ||
+ | ==Two ways to be big== | ||
<div class='qu'> | <div class='qu'> | ||
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. | 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. | ||
Line 112: | Line 115: | ||
<div> | <div> | ||
+ | ==One or the other (but not both)== | ||
<div class='qu'> | <div class='qu'> | ||
<p class=imper>Exclusive OR (XOR). Show the countries that are big by area or big by population but not both. Show name, population and area.</p> | <p class=imper>Exclusive OR (XOR). Show the countries that are big by area or big by population but not both. Show name, population and area.</p> | ||
Line 129: | Line 133: | ||
</div> | </div> | ||
+ | ==Rounding== | ||
<div class='qu'> | <div class='qu'> | ||
Show the <code>name</code> and <code>population</code> in millions and the GDP in billions for the countries of the <code>continent</code> 'South America'. Use the [[ROUND]] function to show the values to two decimal places. | Show the <code>name</code> and <code>population</code> in millions and the GDP in billions for the countries of the <code>continent</code> 'South America'. Use the [[ROUND]] function to show the values to two decimal places. | ||
Line 142: | Line 147: | ||
</source> | </source> | ||
</div> | </div> | ||
+ | <div class=gamelink style='padding:1ex;border:solid thin gray;border-radius:0.5ex'>[http://sqlzoo.net/40289347/sql3/ Click here to play the SELECT card game]</div> | ||
+ | ==Trillion dollar economies== | ||
<div class='qu'> | <div class='qu'> | ||
Show the <code>name</code> 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 the <code>name</code> 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. | ||
Line 156: | Line 163: | ||
</div> | </div> | ||
− | == | + | ==Name and capital have the same length== |
<div class='qu'> | <div class='qu'> | ||
− | + | Greece has capital Athens. | |
− | < | + | |
+ | Each of the strings 'Greece', and 'Athens' has 6 characters. | ||
+ | |||
+ | <p class=imper>Show the name and capital where the name and the capital have the same number of characters.</p> | ||
+ | *You can use the [[LENGTH]] function to find the number of characters in a string | ||
+ | |||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
− | SELECT name, continent, | + | SELECT name, LENGTH(name), continent, LENGTH(continent), capital, LENGTH(capital) |
− | |||
− | |||
FROM world | FROM world | ||
− | WHERE name LIKE ' | + | WHERE name LIKE 'G%' |
</source> | </source> | ||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
− | SELECT name, | + | SELECT name, capital |
− | |||
FROM world | FROM world | ||
− | WHERE name | + | WHERE LENGTH(name)=LENGTH(capital) |
</source> | </source> | ||
</div> | </div> | ||
+ | ==Matching name and capital== | ||
<div class='qu'> | <div class='qu'> | ||
− | <div class=imper>Show the name and the | + | The capital of Sweden is Stockholm. Both words start with the letter 'S'. |
− | + | <div class=imper>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.</div> | ||
+ | *You can use the function [[LEFT]] to isolate the first character. | ||
+ | *You can use <code><></code> as the '''NOT EQUALS''' operator. | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
+ | SELECT name, LEFT(name,1), capital | ||
+ | FROM world | ||
</source> | </source> | ||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
− | SELECT name, | + | SELECT name,capital |
− | + | FROM world | |
− | + | WHERE LEFT(name,1)=LEFT(capital,1) | |
− | + | AND name<>capital | |
− | |||
</source> | </source> | ||
</div> | </div> | ||
+ | ==All the vowels== | ||
<div class='qu'> | <div class='qu'> | ||
− | + | '''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. | |
− | + | ||
− | + | <p class=imper>Find the country that has all the vowels and no spaces in its name.</p> | |
− | + | *You can use the phrase <code>name NOT LIKE '%a%'</code> to exclude characters from your results. | |
− | + | *The query shown misses countries like Bahamas and Belarus because they contain at least one 'a' | |
− | |||
− | |||
− | |||
− | |||
− | |||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
− | SELECT name | + | SELECT name |
− | + | FROM world | |
− | WHERE | + | WHERE name LIKE 'B%' |
+ | AND name NOT LIKE '%a%' | ||
</source> | </source> | ||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
− | SELECT name | + | SELECT name |
− | |||
− | |||
− | |||
− | |||
− | |||
FROM world | FROM world | ||
− | WHERE | + | WHERE name LIKE '%a%' |
+ | AND name LIKE '%e%' | ||
+ | AND name LIKE '%i%' | ||
+ | AND name LIKE '%o%' | ||
+ | AND name LIKE '%u%' | ||
+ | AND name NOT LIKE '% %' | ||
+ | </source> | ||
</div> | </div> | ||
<h2>What Next</h2> | <h2>What Next</h2> | ||
+ | <div class=gamelink style='padding:1ex;border:solid thin gray;border-radius:0.5ex'>[http://sqlzoo.net/40289347/sql3/ Click here to play the SELECT card game]</div> | ||
<ul> | <ul> | ||
<li>[[BBC QUIZ]]</li> | <li>[[BBC QUIZ]]</li> | ||
− | |||
− | |||
− | |||
<li>You can to continue practising the the same techniques | <li>You can to continue practising the the same techniques | ||
and gain more experience of the basic skills on the Nobel table. | and gain more experience of the basic skills on the Nobel table. |
Latest revision as of 13:23, 22 August 2017
Language: | English • 日本語 • 中文 |
---|
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 |
... |
In this tutorial you will use the SELECT command on the table world
:
Contents
- 1 Introduction
- 2 Large Countries
- 3 Per capita GDP
- 4 South America In millions
- 5 France, Germany, Italy
- 6 United
- 7 Two ways to be big
- 8 One or the other (but not both)
- 9 Rounding
- 10 Trillion dollar economies
- 11 Name and capital have the same length
- 12 Matching name and capital
- 13 All the vowels
- 14 What Next
Introduction
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
SELECT name, continent, population FROM world
Large Countries
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.
SELECT name FROM world
WHERE population = 64105700
SELECT name FROM world
WHERE population>200000000
Per capita GDP
Give the name
and the per capita GDP for those countries with a population
of at least 200 million.
per capita GDP is the GDP divided by the population GDP/population
SELECT name, gdp/population FROM world
WHERE population > 200000000
South America In millions
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.
SELECT name, population/1000000 FROM world
WHERE continent='South America'
France, Germany, Italy
Show the name
and population
for France, Germany, Italy
SELECT name, population FROM world
WHERE name IN ('France','Germany','Italy')
United
Show the countries which have a name
that includes the word 'United'
SELECT name FROM world
WHERE name LIKE '%United%'
Two ways to be big
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.
select name,population,area
from world
where area>3000000
or population>250000000
One or the other (but not both)
Exclusive OR (XOR). Show the countries that are big by area or big by population but not both. Show name, population and area.
- Australia has a big area but a small population, it should be included.
- Indonesia has a big population but a small area, it should be included.
- China has a big population and big area, it should be excluded.
- United Kingdom has a small population and a small area, it should be excluded.
select name, population,area
from world
where
(population>250000000 or area>3000000)
and not(population>250000000 and area>3000000)
Rounding
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.
SELECT name, ROUND(population/1000000,2),
ROUND(gdp/1000000000,2)
FROM world
WHERE continent='South America'
Trillion dollar economies
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
Name and capital have the same length
Greece has capital Athens.
Each of the strings 'Greece', and 'Athens' has 6 characters.
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
SELECT name, LENGTH(name), continent, LENGTH(continent), capital, LENGTH(capital)
FROM world
WHERE name LIKE 'G%'
SELECT name, capital
FROM world
WHERE LENGTH(name)=LENGTH(capital)
Matching name and capital
The capital of Sweden is Stockholm. Both words start with the letter 'S'.
- You can use the function LEFT to isolate the first character.
- You can use
<>
as the NOT EQUALS operator.
SELECT name, LEFT(name,1), capital
FROM world
SELECT name,capital
FROM world
WHERE LEFT(name,1)=LEFT(capital,1)
AND name<>capital
All the vowels
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 'B%'
AND name NOT LIKE '%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 '% %'
What Next
- 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 thenobel
table. - You can learn about nested statements, these are instructive and entertaining, but not essential for beginners.
Nested
SELECT
statements using theworld
table.