Difference between revisions of "SELECT basics"
From SQLZOO
(→Introducing the BBC Table of Countries) |
(→Introducing the BBC Table of Countries) |
||
| Line 77: | Line 77: | ||
<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/> | ||
| − | + | <b>We use <code>AND</code> to ensure that two or more conditions hold | |
| − | + | true.</b> | |
| − | + | <div>The example shows the countries where the population is small and the | |
| − | + | gdp is high.</div> | |
| − | + | <div class='imper'>Show the <b>name</b> and <b>region</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 , region | SELECT name , region | ||
Revision as of 14:15, 6 July 2012
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:
| 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 | |
| ... | ||||
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'
This query shows the population density
population/area
for each country where the area is over 5,000,000 km2.Show the per capita gdp:
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
Where to find some very small, very rich countries.
We use AND to ensure that two or more conditions hold
true.
The example shows the countries where the population is small and the
gdp is high.
Show the name and region where the area is less then 2000 and the gdp is more than 5000000000
SELECT name , region FROM bbc WHERE population < 2000000 AND gdp > 5000000000
SELECT name , region FROM bbc WHERE area < 2000 AND gdp > 5000000000