Difference between revisions of "SQLZOO:SELECT basics"

From SQLZOO
Jump to: navigation, search
(Introducing the world table of countries)
 
(109 intermediate revisions by 38 users not shown)
Line 1: Line 1:
 +
{{Languages}}
 
<div class="ref_section">
 
<div class="ref_section">
 
<table class='db_ref'>
 
<table class='db_ref'>
Line 9: Line 10:
 
<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>
+
<td colspan='5'>....</td>
 
</tr>
 
</tr>
 
</table>
 
</table>
 
</div>
 
</div>
 
 
==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 27: Line 27:
 
    
 
    
 
<div class='qu'>
 
<div class='qu'>
The example shows the population of 'France'.
+
The example uses a WHERE clause to show the population of 'France'.
Strings should be in 'single quotes';
+
Note that strings (pieces of text that are data) should be in 'single quotes';
  
<p class='imper'>Show the population of Germany</p>
+
<p class='imper'>Modify it to show the population of Germany</p>
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
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 name and population density
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>. Population density is not a column in the World table, but we
<div class='imper'>Show the per capita gdp: <code>gdp/population</code>
+
can calculate it as <code>population/area</code>.</div>
for each country where the area is over 5,000,000 km<sup>2</sup></div>
+
<div class='imper'>Modify it to 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>.
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 57: Line 58:
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
 
SELECT name, gdp/population FROM world
 
SELECT name, gdp/population FROM world
   WHERE area > 5000000
+
   WHERE area > '5000000'
 
</source>
 
</source>
</div>
 
  
==Small and wealthy==
 
<div class='qu'>
 
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>continent</b> where the area is less then 2000 and the gdp is more than 5000000000</div>
 
<source lang='sql' class='def'>
 
SELECT name , continent
 
  FROM world
 
  WHERE population < 2000000
 
    AND gdp > 5000000000
 
</source>
 
 
<source lang='sql' class='ans'>
 
SELECT name , continent
 
  FROM world
 
  WHERE area < 2000
 
    AND gdp > 5000000000
 
</source>
 
 
</div>
 
</div>
  
Line 87: Line 66:
 
<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.  
The example shows the name and population for the countries 'Ireland', 'Iceland' and 'Denmark'
+
The example shows the name and population for the countries 'Luxembourg', 'Mauritius' and 'Samoa'.
<div class='imper'>Show the '''name''' and the '''population''' for 'Denmark', 'Finland', 'Norway', 'Sweden'
+
<div class='imper'>Show the '''name''' and the '''population''' for 'Ireland', 'Iceland' and 'Denmark'.</div>
</div>
+
<source lang='sql' class='def'>
+
SELECT name, population FROM world
+
  WHERE name IN ('Ireland', 'Iceland',
+
                'Denmark')</source>
+
  
<source lang='sql' class='ans'>
 
SELECT name, population FROM world
 
  WHERE name IN ('Denmark', 'Finland',
 
                'Norway', 'Sweden')</source>
 
</div>
 
  
==Starts with G==
 
<div class='qu'>
 
What are the countries beginning with G?
 
'''The word''' <code>LIKE</code> '''permits pattern matching''' - % '''is the wildcard'''.
 
The examples shows countries beginning with D
 
<div class='imper'>Show each country that begins with G</div>
 
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT name FROM world
+
SELECT name, population FROM world
   WHERE name LIKE 'D%'
+
   WHERE name IN ('Luxembourg', 'Mauritius', 'Samoa');
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT name FROM world
+
SELECT name, population FROM world
   WHERE name LIKE 'G%'
+
   WHERE name IN ('Ireland', 'Iceland', 'Denmark');
 
</source>
 
</source>
 
</div>
 
</div>
 +
 +
<div> 
 +
<p><div class="quizlink">[[SELECT Quiz]]</div></p>
 +
</div>
 +
 +
<div class='quizlink'>[http://socweb8.napier.ac.uk/~40052114/regex/index.php?title=How_To_Play Try the regular expressions game.]</div>
 +
 +
<p>You are ready for tutorial one:[[SELECT_from_WORLD_Tutorial |SELECT statements with WHERE.]]</p>
 +
 +
 +
{{Languages}}
  
 
==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?  
<code>BETWEEN</code> allows range checking - note that it is inclusive.
+
<code>BETWEEN</code> allows range checking (range specified is inclusive of boundary values). The example below shows countries with an area of 250,000-300,000 sq. km. Modify it to show the country and the area for countries with an area between 200,000 and 250,000.
<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 FROM world
   WHERE area BETWEEN 207600 AND 244820
+
   WHERE area BETWEEN 200000 AND 250000
 
</source>
 
</source>
</div>
 
  
<div>
 
<div class="lsclear">Clear your results</div>
 
<p><div class="quizlink">[[SELECT Quiz]]</div></p>
 
 
</div>
 
</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 23:17, 3 May 2016

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

Introducing the world table of countries

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

Summary

The example uses a WHERE clause to show the population of 'France'. Note that strings (pieces of text that are data) should be in 'single quotes';

Modify it to show the population of Germany

SELECT population FROM world
  WHERE name = 'France'
SELECT population FROM world
  WHERE name = 'Germany'

Per Capita GDP

The query shows the name and population density

for each country where the area is over 5,000,000 km2. Population density is not a column in the World table, but we

can calculate it as population/area.
Modify it to 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'

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 'Luxembourg', 'Mauritius' and 'Samoa'.

Show the name and the population for 'Ireland', 'Iceland' and 'Denmark'.


SELECT name, population FROM world
  WHERE name IN ('Luxembourg', 'Mauritius', 'Samoa');
SELECT name, population FROM world
  WHERE name IN ('Ireland', 'Iceland', 'Denmark');

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


Language: English

Just the right size

Which countries are not too small and not too big? BETWEEN allows range checking (range specified is inclusive of boundary values). The example below shows countries with an area of 250,000-300,000 sq. km. Modify it to show the country and the area for countries with an area between 200,000 and 250,000.

SELECT name, area FROM world
  WHERE area BETWEEN 250000 AND 300000


SELECT name, area FROM world
  WHERE area BETWEEN 200000 AND 250000


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


Language: English