Difference between revisions of "SELECT from WORLD Tutorial"

From SQLZOO
Jump to: navigation, search
 
(34 intermediate revisions by 9 users not shown)
Line 14: Line 14:
 
</div>
 
</div>
  
==World Country Profiles==
+
==Country Profile==
 
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>
  
<div class="progress_panel"><div>
 
  <div class="summary">Summary</div>
 
  <div class="progressbarbg">
 
    <div class="progressbar"></div>
 
  </div>
 
</div></div>
 
 
==Warming up==
 
 
<div class='qu'>
 
<div class='qu'>
[[Read_the_notes_about_this_table. |Read the notes about this table.]] Observe the result of running a simple SQL command.
+
[[Read_the_notes_about_this_table. |Read the notes about this table.]] Observe the result of running a simple SQL command.  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
SELECT name, continent, population FROM world
 
SELECT name, continent, population FROM world
Line 38: Line 30:
 
</div>
 
</div>
  
==Large Countries==
 
[[WHERE_filters |How to use WHERE to filter records.]]
 
 
<div class='qu'>
 
<div class='qu'>
Show the name for the countries  that have a population of at least 200 million. (200 million is 200000000, there are eight zeros)
+
[[WHERE_filters |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.
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
SELECT name FROM world
 
SELECT name FROM world
Line 55: Line 47:
  
 
<div class='qu'>
 
<div class='qu'>
Give the name and the per capita GDP for those countries with a population 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.  
 
<div title="HELP:How to calculate per capita GDP" class='hint'>
 
<div title="HELP:How to calculate per capita GDP" class='hint'>
 
per capita GDP is the GDP divided by the population GDP/population  
 
per capita GDP is the GDP divided by the population GDP/population  
Line 69: Line 61:
  
 
<div class='qu'>
 
<div class='qu'>
Show the <code>name</code> and <code>population</code> in millions for the countries of 'South America'
+
Show the <code>name</code> and <code>population</code> in millions for the countries of the <code>continent</code> 'South America'.
 
Divide the population by 1000000 to get population in millions.  
 
Divide the population by 1000000 to get population in millions.  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 81: Line 73:
  
 
<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
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 92: Line 84:
  
 
<div class='qu'>
 
<div class='qu'>
Identify the countries which have names including the word 'United'
+
Show the countries which have a <code>name</code> that includes the word 'United'
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 102: Line 94:
 
</div>
 
</div>
 
<div>
 
<div>
<div class="lsclear">Clear your results</div>
+
 
<p><div class="quizlink">[[BBC QUIZ]]</div></p>
+
<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.
 +
<p class=imper>Show the countries that are big by area or big by population. Show name, population and area.</p>  
 +
<source lang='sql' class='def'>
 +
</source>
 +
 
 +
<source lang='sql' class='ans'>
 +
select name,population,area
 +
from world
 +
where area>3000000
 +
or population>250000000
 +
</source>
 
</div>
 
</div>
 +
<div>
 +
 +
<div class='qu'>
 +
USA and China are big in population '''and''' big by area. Exclude these countries.
 +
<p class=imper>Show the countries that are big by area or big by population but not both. Show name, population and area.</p>
 +
<source lang='sql' class='def'>
 +
</source>
 +
<source lang='sql' class='ans'>
 +
select name, population,area
 +
from world
 +
where
 +
(population>250000000 or area>3000000)
 +
and not(population>250000000 and area>3000000)
 +
</source>
 +
</div>
 +
 +
<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.
 +
<div class=imper>For South America show population in millions and GDP in billions to 2 decimal places.</div>
 +
<div class=hint title='Millions and billions'>Divide by 1000000 (6 zeros) for millions. Divide by 1000000000 (9 zeros) for billions.</div>
 +
<source lang='sql' class='def'>
 +
</source>
 +
<source lang='sql' class='ans'>
 +
SELECT name, ROUND(population/1000000,2),
 +
            ROUND(gdp/1000000000,2)
 +
  FROM world
 +
WHERE continent='South America'
 +
</source>
 +
</div>
 +
 +
<div class='qu'>
 +
Show the 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.
 +
<p class=imper>Show per-capita GDP for the trillion dollar countries to the nearest $1000.</p>
 +
<source lang='sql' class='def'>
 +
</source>
 +
<source lang='sql' class='ans'>
 +
select name, ROUND(gdp/population,-3)
 +
from world
 +
where
 +
gdp>1000000000000
 +
</source>
 +
</div>
 +
 +
==Harder Questions==
 +
<div class='qu'>
 +
The [[CASE]] statement shown is used to substitute '''North America''' for '''Caribbean''' in  the third column.
 +
<div class=imper>Show the name and the continent - but substitute '''Australasia''' for '''Oceania''' - for countries beginning with N.</div>
 +
<source lang='sql' class='def'>
 +
SELECT name, continent,
 +
      CASE WHEN continent='Caribbean' THEN 'North America'
 +
            ELSE continent END
 +
  FROM world
 +
WHERE name LIKE 'J%'
 +
</source>
 +
<source lang='sql' class='ans'>
 +
SELECT name, CASE WHEN continent='Oceania' THEN 'Australasia'
 +
                  ELSE continent END
 +
  FROM world
 +
WHERE name LIKE 'N%'
 +
</source>
 +
</div>
 +
 +
<div class='qu'>
 +
<div class=imper>Show the name and the continent - but substitute '''Eurasia''' for Europe and Asia; substitute '''America''' - for each country in '''North America'''
 +
or '''South America''' or '''Caribbean'''. Show countries beginning with A or B</div>
 +
<source lang='sql' class='def'>
 +
</source>
 +
<source lang='sql' class='ans'>
 +
SELECT name, CASE WHEN continent IN ('Europe','Asia') THEN 'Eurasia'
 +
                  WHEN continent IN ('North America','South America','Caribbean') THEN 'America'
 +
                  ELSE continent END
 +
  FROM world
 +
WHERE name BETWEEN 'A' AND 'C'
 +
</source>
 +
</div>
 +
 +
<div class='qu'>
 +
Put the continents right...
 +
<ul>
 +
  <li>Oceania becomes Australasia</li>
 +
  <li>Countries in Eurasia and Turkey go to '''Europe/Asia'''</li>
 +
  <li>Caribbean islands starting with 'B' go to '''North America''', other Caribbean islands go to '''South America'''</li>
 +
</ul>
 +
<div class=imper>Show the name, the original continent and the new continent of all countries.</div>
 +
<source lang='sql' class='def'>
 +
</source>
 +
<source lang='sql' class='ans'>
 +
SELECT name,continent,
 +
  CASE WHEN continent = 'Eurasia' OR name='Turkey' THEN 'Europe/Asia'
 +
      WHEN continent IN ('Oceania') THEN 'Australasia'
 +
      WHEN continent = 'Caribbean' AND name LIKE 'B%' THEN 'North America'
 +
      WHEN continent = 'Caribbean' THEN 'South America'
 +
      ELSE continent END
 +
  FROM world
 +
ORDER BY name</source>
 +
</div>
 +
 +
 
<h2>What Next</h2>
 
<h2>What Next</h2>
 
<ul>
 
<ul>
 +
  <li>[[BBC QUIZ]]</li>
 
   <li>You can play a game:
 
   <li>You can play a game:
   [http://sqlzoo.net/~andrew/brain/bt.htm Find the duplicate]
+
   [http://sqlzoo.net/brain/bt.htm Find the duplicate]
 
   </li>
 
   </li>
 
   <li>You can to continue practising the the same techniques
 
   <li>You can to continue practising the the same techniques
Line 118: Line 220:
 
   [[SELECT_within_SELECT_Tutorial |Nested <code>SELECT</code> statements using the
 
   [[SELECT_within_SELECT_Tutorial |Nested <code>SELECT</code> statements using the
 
   <code>world</code> table.]]</li>
 
   <code>world</code> table.]]</li>
</ul>
+
</ul>

Latest revision as of 07:27, 7 May 2015

namecontinentarea populationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748 2831741 12960000000
AlgeriaAfrica2381741 37100000 188681000000
AndorraEurope46878115 3712000000
AngolaAfrica1246700 20609294 100990000000
...

Country Profile

In this tutorial you will use the SELECT command on the table World:

Read the notes about this table. Observe the result of running a simple SQL command.

SELECT name, continent, population FROM world
SELECT name, continent, population FROM world

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>250000000
SELECT name FROM world
WHERE population>200000000


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

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'

Show the name and population for France, Germany, Italy

 
SELECT name, population FROM world
  WHERE name IN ('France','Germany','Italy')

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: 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

USA and China are big in population and big by area. Exclude these countries.

Show the countries that are big by area or big by population but not both. Show name, population and area.

 
SELECT name, population,area
FROM world
WHERE
(population>250000000 OR area>3000000)
AND NOT(population>250000000 AND area>3000000)

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 to 2 decimal places.
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'

Show the 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

Harder Questions

The CASE statement shown is used to substitute North America for Caribbean in the third column.

Show the name and the continent - but substitute Australasia for Oceania - for countries beginning with N.
SELECT name, continent,
       CASE WHEN continent='Caribbean' THEN 'North America'
            ELSE continent END
  FROM world
 WHERE name LIKE 'J%'
SELECT name, CASE WHEN continent='Oceania' THEN 'Australasia'
                  ELSE continent END
  FROM world
 WHERE name LIKE 'N%'
Show the name and the continent - but substitute Eurasia for Europe and Asia; substitute America - for each country in North America or South America or Caribbean. Show countries beginning with A or B
 
SELECT name, CASE WHEN continent IN ('Europe','Asia') THEN 'Eurasia'
                  WHEN continent IN ('North America','South America','Caribbean') THEN 'America'
                  ELSE continent END
  FROM world
 WHERE name BETWEEN 'A' AND 'C'

Put the continents right...

  • Oceania becomes Australasia
  • Countries in Eurasia and Turkey go to Europe/Asia
  • Caribbean islands starting with 'B' go to North America, other Caribbean islands go to South America
Show the name, the original continent and the new continent of all countries.
 
SELECT name,continent,
  CASE WHEN continent = 'Eurasia' OR name='Turkey' THEN 'Europe/Asia'
       WHEN continent IN ('Oceania') THEN 'Australasia'
       WHEN continent = 'Caribbean' AND name LIKE 'B%' THEN 'North America'
       WHEN continent = 'Caribbean' THEN 'South America'
       ELSE continent END
  FROM world
ORDER BY name


What Next