Difference between revisions of "SQLZOO:SELECT basics"
(37 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
− | = | + | <div class="ref_section"> |
− | + | <table class='db_ref'> | |
− | <table | + | <tr><th>name</th><th>region</th><th>area</th> |
− | <th>name</th> | + | <th>population</th><th>gdp</th></tr> |
− | <th>region</th> | + | <tr><td>Afghanistan</td><td>South Asia</td><td class="r">652225</td><td class="r">26000000</td><td class="r"></td></tr> |
− | <th>area</th> | + | <tr><td>Albania</td><td>Europe</td><td class="r">28728</td><td class="r">3200000</td><td class="r">6656000000</td></tr> |
− | <th>population</th> | + | <tr><td>Algeria</td><td>Middle East</td><td class="r">2400000</td><td class="r">32900000</td><td class="r">75012000000</td></tr> |
− | <th>gdp</th> | + | <tr><td>Andorra</td><td>Europe</td><td class="r">468</td><td class="r">64000</td><td class="r"></td></tr><tr> |
− | </tr> | + | <td>Angola</td><td>Africa</td><td class="r">1250000</td><td class="r">14500000</td><td class="r">14935000000</td></tr> |
− | <tr> | ||
− | <td>Afghanistan</td> | ||
− | <td>South Asia</td> | ||
− | <td | ||
− | <td | ||
− | <td></td> | ||
− | </tr> | ||
− | <tr> | ||
− | <td>Albania</td> | ||
− | <td>Europe</td> | ||
− | <td | ||
− | <td | ||
− | <td | ||
− | </tr> | ||
− | <tr> | ||
− | <td>Algeria</td> | ||
− | <td>Middle East</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> | ||
</tr> | </tr> | ||
</table> | </table> | ||
+ | </div> | ||
+ | ==Introducing the BBC Table of Countries== | ||
+ | <p>This tutorial introduces SQL as a query language. We will be using the SELECT command on the table '''bbc''':</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='qu'> | <div class='qu'> | ||
The example shows the population of 'France'. | The example shows the population of 'France'. | ||
Line 58: | Line 43: | ||
</div> | </div> | ||
+ | ==Per Capita GDP== | ||
<div class='qu'> | <div class='qu'> | ||
<div>This query shows the population density <code>population/area</code> | <div>This query shows the population density <code>population/area</code> | ||
Line 75: | Line 61: | ||
</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 97: | 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 114: | Line 101: | ||
</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 130: | Line 118: | ||
</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 144: | Line 133: | ||
</source> | </source> | ||
</div> | </div> | ||
+ | |||
+ | <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_BBC_Tutorial |SELECT statements with WHERE.]]</p> | ||
+ | |||
+ | |||
+ | {{Languages}} |
Revision as of 15:50, 28 October 2012
name | region | area | population | gdp |
---|---|---|---|---|
Afghanistan | South Asia | 652225 | 26000000 | |
Albania | Europe | 28728 | 3200000 | 6656000000 |
Algeria | Middle East | 2400000 | 32900000 | 75012000000 |
Andorra | Europe | 468 | 64000 | |
Angola | Africa | 1250000 | 14500000 | 14935000000 |
... |
Contents
Introducing the BBC Table of Countries
This tutorial introduces SQL as a query language. We will be using the SELECT command on the table bbc:
The example shows the population of 'France'. Strings should be in 'single quotes';
Show the population of Germany
SELECT population FROM bbc
WHERE name = 'France'
SELECT population FROM bbc
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 bbc
WHERE area > 5000000
SELECT name, gdp/population FROM bbc
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 , region
FROM bbc
WHERE population < 2000000
AND gdp > 5000000000
SELECT name , region
FROM bbc
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 bbc
WHERE name IN ('Ireland', 'Iceland',
'Denmark')
SELECT name, population FROM bbc
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 bbc
WHERE name LIKE 'D%'
SELECT name FROM bbc
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 bbc
WHERE area BETWEEN 207600 AND 244820
SELECT name, area/1000 FROM bbc
WHERE area BETWEEN 207600 AND 244820
You are ready for tutorial one:SELECT statements with WHERE.
Language: | English |
---|