Difference between revisions of "SELECT basics"
| Line 41: | Line 41: | ||
</div> | </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> | <p>This tutorial introduces SQL as a query language. We will be using the SELECT command on the table '''bbc''':</p> | ||
| Line 70: | Line 70: | ||
</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 87: | Line 88: | ||
</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 109: | Line 111: | ||
</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 125: | Line 128: | ||
</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 141: | Line 145: | ||
</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? | ||
Revision as of 12:00, 22 August 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 | |
| ... | ||||
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 • Deutsch |
|---|