Difference between revisions of "SQLZOO:SELECT basics"

From SQLZOO
Jump to: navigation, search
Line 1: Line 1:
 
<div class="ref_section">
 
<div class="ref_section">
 
<table class='db_ref'>
 
<table class='db_ref'>
<tr><th>name</th><th>region</th><th>area</th>
+
<tr><th>name</th><th>continent</th><th>area</th>
 
<th>population</th><th>gdp</th></tr>
 
<th>population</th><th>gdp</th></tr>
 
<tr><td>Afghanistan</td><td>South Asia</td><td class="r">652225</td><td class="r">26000000</td><td class="r"></td></tr>
 
<tr><td>Afghanistan</td><td>South Asia</td><td class="r">652225</td><td class="r">26000000</td><td class="r"></td></tr>
Line 14: Line 14:
 
</div>
 
</div>
  
==Introducing the BBC Table of Countries==
+
==Introducing the <code>world</code> 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 '''world''':</p>
  
 
<div class="progress_panel"><div>
 
<div class="progress_panel"><div>
Line 33: Line 33:
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT population FROM bbc
+
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 bbc
+
SELECT population FROM world
 
   WHERE name = 'Germany'
 
   WHERE name = 'Germany'
 
</source>
 
</source>
Line 51: Line 51:
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT name, population/area FROM bbc
+
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 bbc
+
SELECT name, gdp/population FROM world
 
   WHERE area > 5000000
 
   WHERE area > 5000000
 
</source>
 
</source>
Line 68: 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>region</b> where the area is less then 2000 and the gdp is more than 5000000000</div>
+
<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 , region
+
SELECT name , continent
   FROM bbc
+
   FROM world
 
   WHERE population < 2000000
 
   WHERE population < 2000000
 
     AND gdp > 5000000000
 
     AND gdp > 5000000000
Line 77: Line 77:
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT name , region
+
SELECT name , continent
   FROM bbc
+
   FROM world
 
   WHERE area < 2000
 
   WHERE area < 2000
 
     AND gdp > 5000000000
 
     AND gdp > 5000000000
Line 91: Line 91:
 
</div>
 
</div>
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT name, population FROM bbc
+
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 bbc
+
SELECT name, population FROM world
 
   WHERE name IN ('Denmark', 'Finland',
 
   WHERE name IN ('Denmark', 'Finland',
 
                 'Norway', 'Sweden')</source>
 
                 'Norway', 'Sweden')</source>
Line 108: 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 bbc
+
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 bbc
+
SELECT name FROM world
 
   WHERE name LIKE 'G%'
 
   WHERE name LIKE 'G%'
 
</source>
 
</source>
Line 124: 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 bbc
+
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 bbc
+
SELECT name, area/1000 FROM world
 
   WHERE area BETWEEN 207600 AND 244820
 
   WHERE area BETWEEN 207600 AND 244820
 
</source>
 
</source>

Revision as of 21:58, 8 December 2012

namecontinentarea populationgdp
AfghanistanSouth Asia65222526000000
AlbaniaEurope2872832000006656000000
AlgeriaMiddle East24000003290000075012000000
AndorraEurope46864000
AngolaAfrica12500001450000014935000000
...

Contents

Introducing the world table of countries

This tutorial introduces SQL as a query language. We will be using the SELECT command on the table world:

Summary

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

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 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.

The example shows the countries where the population is small and the gdp is high.
Show the name and continent where the area is less then 2000 and the gdp is more than 5000000000
SELECT name , continent
  FROM world
  WHERE population < 2000000
    AND gdp > 5000000000
SELECT name , continent
  FROM world
  WHERE area < 2000
    AND gdp > 5000000000

Scandinavia

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'

Show the name and the population for 'Denmark', 'Finland', 'Norway', 'Sweden'
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

Show each country that begins with G
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.

Show the area in 1000 square km. Show area/1000 instead of area
SELECT name, area FROM world
  WHERE area BETWEEN 207600 AND 244820
SELECT name, area/1000 FROM world
  WHERE area BETWEEN 207600 AND 244820
Clear your results

You are ready for tutorial one:SELECT statements with WHERE.


Language: English
Personal tools

Variants
Actions
Reference
Toolbox
Google AdSense