Difference between revisions of "SQLZOO:SELECT basics"

From SQLZOO
Jump to: navigation, search
 
(58 intermediate revisions by 23 users not shown)
Line 9: Line 9:
 
<td>Angola</td><td>Africa</td><td class="r">1246700 </td><td class="r">20609294 </td><td class="r">100990000000 </td></tr>
 
<td>Angola</td><td>Africa</td><td class="r">1246700 </td><td class="r">20609294 </td><td class="r">100990000000 </td></tr>
 
<tr>
 
<tr>
<td colspan='5'>...</td>[[File:[[File:Example.jpg]][[File:[[File:Example.jpg]]'''Bold text''']]]]
+
<td colspan='5'>....</td>
 
</tr>
 
</tr>
 
</table>
 
</table>
Line 15: Line 15:
  
 
==Introducing the <code>world</code> 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 '''world''':</p>
+
   <p>This tutorial introduces SQL. We will be using the SELECT command on the table '''world''':</p>
  
 
<div class="progress_panel"><div>
 
<div class="progress_panel"><div>
Line 45: Line 45:
 
==Per Capita GDP==
 
==Per Capita GDP==
 
<div class='qu'>
 
<div class='qu'>
<div>This query shows the population density <code>population/area</code>
+
<div>The 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>
 
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>
+
<div class='imper'>Show the name and per capita gdp: <code>gdp/population</code>
for each country where the area is over 5,000,000 km<sup>2</sup></div>
+
for each country where the area is over 5,000,000 km<sup>2</sup></div>.
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 59: Line 59:
 
   WHERE area > 5000000
 
   WHERE area > 5000000
 
</source>
 
</source>
 +
 
</div>
 
</div>
  
Line 68: Line 69:
 
<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>continent</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 than 2000 and the gdp is more than 5000000000</div>
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
SELECT name , continent
 
SELECT name , continent
Line 120: Line 121:
 
==Just the right size==
 
==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? Show the country and the area for countries with an area between 200000 and 250000.
 
<code>BETWEEN</code> allows range checking - note that it is inclusive.
 
<code>BETWEEN</code> allows range checking - note that it is inclusive.
 
<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 world
 
SELECT name, area FROM world
   WHERE area BETWEEN 207600 AND 244820
+
   WHERE area BETWEEN 250000 AND 300000
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
 
SELECT name, area/1000 FROM world
 
SELECT name, area/1000 FROM world
   WHERE area BETWEEN 207600 AND 244820
+
   WHERE area BETWEEN 200000 AND 250000
 
</source>
 
</source>
 
</div>
 
</div>
  
<div>
+
<div>
<div class="lsclear">Clear your results</div>
+
 
<p><div class="quizlink">[[SELECT Quiz]]</div></p>
 
<p><div class="quizlink">[[SELECT Quiz]]</div></p>
 
</div>
 
</div>
 +
 +
<div class='quizlink'>[http://socweb8.napier.ac.uk/~40052114/wiki/index.php?title=Range Try the regular expressions game.]</div>
 +
 
<p>You are ready for tutorial one:[[SELECT_from_WORLD_Tutorial |SELECT statements with WHERE.]]</p>
 
<p>You are ready for tutorial one:[[SELECT_from_WORLD_Tutorial |SELECT statements with WHERE.]]</p>
  
  
 
{{Languages}}
 
{{Languages}}

Latest revision as of 16:55, 28 October 2014

namecontinentarea populationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748 2831741 12960000000
AlgeriaAfrica2381741 37100000 188681000000
AndorraEurope46878115 3712000000
AngolaAfrica1246700 20609294 100990000000
....

Contents

[edit] Introducing the world table of countries

This tutorial introduces SQL. 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'

[edit] Per Capita GDP

The query shows the population density population/area for each country where the area is over 5,000,000 km2.
Show the name and 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

[edit] 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 than 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

[edit] 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')

[edit] 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%'

[edit] Just the right size

Which countries are not too small and not too big? Show the country and the area for countries with an area between 200000 and 250000. 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 250000 AND 300000
SELECT name, area/1000 FROM world
  WHERE area BETWEEN 200000 AND 250000

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


Language: English
Personal tools

Variants
Actions
Reference
Toolbox
Google AdSense