Difference between revisions of "SQLZOO:SELECT basics"
(→Per Capita GDP) |
|||
(27 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
− | |||
<div class="ref_section"> | <div class="ref_section"> | ||
− | <table class='db_ref'><tr> | + | <table class='db_ref'> |
− | <th>name</th> | + | <tr><th>name</th><th>continent</th><th>area</th> |
− | <th> | + | <th>population</th><th>gdp</th></tr> |
− | <th>area</th> | + | <tr><td>Afghanistan</td><td>Asia</td><td class="r">652230</td><td class="r">25500100</td><td class="r">20343000000</td></tr> |
− | <th>population</th> | + | <tr><td>Albania</td><td>Europe</td><td class="r">28748 </td><td class="r">2831741 </td><td class="r">12960000000 </td></tr> |
− | <th>gdp</th> | + | <tr><td>Algeria</td><td>Africa</td><td class="r">2381741 </td><td class="r">37100000 </td><td class="r">188681000000 </td></tr> |
− | </tr> | + | <tr><td>Andorra</td><td>Europe</td><td class="r">468</td><td class="r">78115 </td><td class="r">3712000000 </td></tr><tr> |
− | <tr> | + | <td>Angola</td><td>Africa</td><td class="r">1246700 </td><td class="r">20609294 </td><td class="r">100990000000 </td></tr> |
− | <td>Afghanistan</td> | ||
− | <td> | ||
− | <td | ||
− | <td | ||
− | <td></td> | ||
− | </tr> | ||
− | <tr> | ||
− | <td>Albania</td> | ||
− | <td>Europe</td> | ||
− | <td | ||
− | <td | ||
− | <td | ||
− | </tr> | ||
− | <tr> | ||
− | <td>Algeria</td> | ||
− | <td> | ||
− | <td | ||
− | <td | ||
− | <td | ||
− | </tr> | ||
− | <tr> | ||
− | <td>Andorra</td> | ||
− | <td>Europe</td> | ||
− | <td | ||
− | <td | ||
− | <td></td> | ||
− | </tr> | ||
<tr> | <tr> | ||
<td colspan='5'>...</td> | <td colspan='5'>...</td> | ||
Line 42: | Line 14: | ||
</div> | </div> | ||
− | + | ==Introducing the <code>world</code> table of countries== | |
− | <p>This tutorial introduces SQL | + | <p>This tutorial introduces SQL. We will be using the SELECT command on the table '''world''':</p> |
+ | |||
+ | <div class="progress_panel"><div> | ||
+ | <div class="summary">Summary</div> | ||
+ | <div class="progressbarbg"> | ||
+ | <div class="progressbar"></div> | ||
+ | </div> | ||
+ | </div></div> | ||
<div class='extra_space' style='width:1em; height:6em;'></div> | <div class='extra_space' style='width:1em; height:6em;'></div> | ||
Line 54: | Line 33: | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
− | SELECT population FROM | + | SELECT population FROM world |
WHERE name = 'France' | WHERE name = 'France' | ||
</source> | </source> | ||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
− | SELECT population FROM | + | SELECT population FROM world |
WHERE name = 'Germany' | WHERE name = 'Germany' | ||
</source> | </source> | ||
</div> | </div> | ||
+ | ==Per Capita GDP== | ||
<div class='qu'> | <div class='qu'> | ||
− | <div> | + | <div>The query shows the population density <code>population/area</code> |
for each country where the area is over 5,000,000 km<sup>2</sup>.</div> | for each country where the area is over 5,000,000 km<sup>2</sup>.</div> | ||
<div class='imper'>Show the per capita gdp: <code>gdp/population</code> | <div class='imper'>Show the per capita gdp: <code>gdp/population</code> | ||
Line 71: | Line 51: | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
− | SELECT name, population/area FROM | + | SELECT name, population/area FROM world |
WHERE area > 5000000 | WHERE area > 5000000 | ||
</source> | </source> | ||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
− | SELECT name, gdp/population FROM | + | SELECT name, gdp/population FROM world |
WHERE area > 5000000 | WHERE area > 5000000 | ||
</source> | </source> | ||
</div> | </div> | ||
+ | ==Small and wealthy== | ||
<div class='qu'> | <div class='qu'> | ||
Where to find some very small, very rich countries.<br/> | Where to find some very small, very rich countries.<br/> | ||
Line 87: | Line 68: | ||
<div>The example shows the countries where the population is small and the | <div>The example shows the countries where the population is small and the | ||
gdp is high.</div> | gdp is high.</div> | ||
− | <div class='imper'>Show the <b>name</b> and <b> | + | <div class='imper'>Show the <b>name</b> and <b>continent</b> where the area is less then 2000 and the gdp is more than 5000000000</div> |
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
− | SELECT name , | + | SELECT name , continent |
− | FROM | + | FROM world |
WHERE population < 2000000 | WHERE population < 2000000 | ||
AND gdp > 5000000000 | AND gdp > 5000000000 | ||
Line 96: | Line 77: | ||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
− | SELECT name , | + | SELECT name , continent |
− | FROM | + | FROM world |
WHERE area < 2000 | WHERE area < 2000 | ||
AND gdp > 5000000000 | AND gdp > 5000000000 | ||
Line 103: | Line 84: | ||
</div> | </div> | ||
− | + | ==Scandinavia== | |
<div class='qu'> | <div class='qu'> | ||
Checking a list The word <b>IN</b> allows us to check if an item is in a list. | Checking a list The word <b>IN</b> allows us to check if an item is in a list. | ||
Line 110: | Line 91: | ||
</div> | </div> | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
− | SELECT name, population FROM | + | SELECT name, population FROM world |
WHERE name IN ('Ireland', 'Iceland', | WHERE name IN ('Ireland', 'Iceland', | ||
'Denmark')</source> | 'Denmark')</source> | ||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
− | SELECT name, population FROM | + | SELECT name, population FROM world |
WHERE name IN ('Denmark', 'Finland', | WHERE name IN ('Denmark', 'Finland', | ||
'Norway', 'Sweden')</source> | 'Norway', 'Sweden')</source> | ||
</div> | </div> | ||
+ | ==Starts with G== | ||
<div class='qu'> | <div class='qu'> | ||
What are the countries beginning with G? | What are the countries beginning with G? | ||
Line 126: | Line 108: | ||
<div class='imper'>Show each country that begins with G</div> | <div class='imper'>Show each country that begins with G</div> | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
− | SELECT name FROM | + | SELECT name FROM world |
WHERE name LIKE 'D%' | WHERE name LIKE 'D%' | ||
</source> | </source> | ||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
− | SELECT name FROM | + | SELECT name FROM world |
WHERE name LIKE 'G%' | WHERE name LIKE 'G%' | ||
</source> | </source> | ||
</div> | </div> | ||
+ | ==Just the right size== | ||
<div class='qu'> | <div class='qu'> | ||
Which countries are not too small and not too big? | Which countries are not too small and not too big? | ||
Line 141: | Line 124: | ||
<div class='imper'>'''Show the area in 1000 square km. Show''' ''area''/1000 '''instead of''' ''area''</div> | <div class='imper'>'''Show the area in 1000 square km. Show''' ''area''/1000 '''instead of''' ''area''</div> | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
− | SELECT name, area FROM | + | SELECT name, area FROM world |
WHERE area BETWEEN 207600 AND 244820 | WHERE area BETWEEN 207600 AND 244820 | ||
</source> | </source> | ||
<source lang='sql' class='ans'> | <source lang='sql' class='ans'> | ||
− | SELECT name, area/1000 FROM | + | SELECT name, area/1000 FROM world |
WHERE area BETWEEN 207600 AND 244820 | WHERE area BETWEEN 207600 AND 244820 | ||
</source> | </source> | ||
</div> | </div> | ||
− | <p>[[SELECT Quiz]]</p> | + | |
− | <p>You are ready for tutorial one:[[ | + | <div> |
+ | <div class="lsclear">Clear your results</div> | ||
+ | <p><div class="quizlink">[[SELECT Quiz]]</div></p> | ||
+ | </div> | ||
+ | <p>You are ready for tutorial one:[[SELECT_from_WORLD_Tutorial |SELECT statements with WHERE.]]</p> | ||
+ | |||
+ | |||
+ | {{Languages}} |
Revision as of 14:32, 29 April 2013
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 |
... |
Contents
Introducing the world
table of countries
This tutorial introduces SQL. We will be using the SELECT command on the table world:
The example shows the population of 'France'. Strings should be in 'single quotes';
Show the population of Germany
SELECT population FROM world
WHERE name = 'France'
SELECT population FROM world
WHERE name = 'Germany'
Per Capita GDP
population/area
for each country where the area is over 5,000,000 km2.gdp/population
for each country where the area is over 5,000,000 km2SELECT name, population/area FROM world
WHERE area > 5000000
SELECT name, gdp/population FROM world
WHERE area > 5000000
Small and wealthy
Where to find some very small, very rich countries.
We use AND
to ensure that two or more conditions hold
true.
SELECT name , continent
FROM world
WHERE population < 2000000
AND gdp > 5000000000
SELECT name , continent
FROM world
WHERE area < 2000
AND gdp > 5000000000
Checking a list The word IN allows us to check if an item is in a list. The example shows the name and population for the countries 'Ireland', 'Iceland' and 'Denmark'
SELECT name, population FROM world
WHERE name IN ('Ireland', 'Iceland',
'Denmark')
SELECT name, population FROM world
WHERE name IN ('Denmark', 'Finland',
'Norway', 'Sweden')
Starts with G
What are the countries beginning with G?
The word LIKE
permits pattern matching - % is the wildcard.
The examples shows countries beginning with D
SELECT name FROM world
WHERE name LIKE 'D%'
SELECT name FROM world
WHERE name LIKE 'G%'
Just the right size
Which countries are not too small and not too big?
BETWEEN
allows range checking - note that it is inclusive.
SELECT name, area FROM world
WHERE area BETWEEN 207600 AND 244820
SELECT name, area/1000 FROM world
WHERE area BETWEEN 207600 AND 244820
You are ready for tutorial one:SELECT statements with WHERE.
Language: | English |
---|