Difference between revisions of "SQLZOO:SELECT basics"

From SQLZOO
Jump to: navigation, search
(Population of Germany)
(Introducing the BBC Table of Countries)
Line 55: Line 55:
 
SELECT population FROM bbc
 
SELECT population FROM bbc
 
   WHERE name = 'Germany'
 
   WHERE name = 'Germany'
 +
</source>
 +
</div>
 +
 +
<div class='qu'>
 +
<div>This 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>
 +
<div class='imper'>Show the per capita gdp: <code>gdp/population</code>
 +
for each country where the area is over 5,000,000 km<sup>2</sup></div>
 +
 +
<source lang='sql' class='def'>
 +
SELECT name, population/area FROM bbc
 +
  WHERE area &gt; 5000000
 +
</source>
 +
 +
<source lang='sql' class='ans'>
 +
SELECT name, gdp/population FROM bbc
 +
  WHERE area &gt; 5000000
 
</source>
 
</source>
 
</div>
 
</div>

Revision as of 14:08, 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 km2
SELECT name, population/area FROM bbc
  WHERE area &gt; 5000000
SELECT name, gdp/population FROM bbc
  WHERE area &gt; 5000000
Personal tools

Variants
Actions
Reference
Toolbox
Google AdSense