Difference between revisions of "SELECT basics"

From SQLZOO
Jump to: navigation, search
m
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{Languages}}
 
{{Languages}}
 
<div class="ref_section">
 
<div class="ref_section">
<table class='db_ref'>
+
<table class='db_ref'><caption>world</caption>
 
<tr><th>name</th><th>continent</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>
Line 14: Line 14:
 
</table>
 
</table>
 
</div>
 
</div>
==Introducing the <code>world</code> table of countries==
+
<div class='extra_space' style='width:1em; height:6em;'></div>
  <p>This  tutorial introduces SQL. We will be using the SELECT command on the table '''world''':</p>
 
  
<div class="progress_panel"><div>
+
==Introducing the <code>world</code> table of countries==
  <div class="summary">Summary</div>
 
  <div class="progressbarbg">
 
    <div class="progressbar"></div>
 
  </div>
 
</div></div>
 
 
 
  <div class='extra_space' style='width:1em; height:6em;'></div>
 
 
 
 
<div class='qu'>
 
<div class='qu'>
 
The example uses a WHERE clause to show the population of 'France'.
 
The example uses a WHERE clause to show the population of 'France'.
Line 41: Line 32:
 
   WHERE name = 'Germany'
 
   WHERE name = 'Germany'
 
</source>
 
</source>
</div>
 
 
==Per Capita GDP==
 
<div class='qu'>
 
<div>The query shows the name and population density
 
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
 
can calculate it as <code>population/area</code>.</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'>
 
SELECT name, population/area FROM world
 
  WHERE area > 5000000
 
</source>
 
 
<source lang='sql' class='ans'>
 
SELECT name, gdp/population FROM world
 
  WHERE area > '5000000'
 
</source>
 
 
 
</div>
 
</div>
  
Line 66: Line 37:
 
<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 'Luxembourg', 'Mauritius' and 'Samoa'.
+
The example shows the name and population for the countries 'Brazil', 'Russia', 'India' and 'China'.
<div class='imper'>Show the '''name''' and the '''population''' for 'Ireland', 'Iceland' and 'Denmark'.</div>
+
<div class='imper'>Show the '''name''' and the '''population''' for 'Sweden', 'Norway' and 'Denmark'.</div>
  
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
SELECT name, population FROM world
 
SELECT name, population FROM world
   WHERE name IN ('Luxembourg', 'Mauritius', 'Samoa');
+
   WHERE name IN ('Brazil', 'Russia', 'India', 'China');
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
 
SELECT name, population FROM world
 
SELECT name, population FROM world
   WHERE name IN ('Ireland', 'Iceland', 'Denmark');
+
   WHERE name IN ('Sweden','Norway', '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==
Line 110: Line 70:
 
</div>
 
</div>
  
 
+
*<div class="quizlink">[[SELECT Quiz]]</div>
<p>You are ready for tutorial one:[[SELECT_from_WORLD_Tutorial |SELECT statements with WHERE.]]</p>
+
*You are ready for tutorial one:[[SELECT_from_WORLD_Tutorial |SELECT statements with WHERE.]]
  
  
 
{{Languages}}
 
{{Languages}}

Latest revision as of 18:25, 16 March 2017

Language: English  • Deutsch • 中文
world
namecontinentarea populationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748 2831741 12960000000
AlgeriaAfrica2381741 37100000 188681000000
AndorraEurope46878115 3712000000
AngolaAfrica1246700 20609294 100990000000
....

Introducing the world table of countries

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'

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 'Brazil', 'Russia', 'India' and 'China'.

Show the name and the population for 'Sweden', 'Norway' and 'Denmark'.


SELECT name, population FROM world
  WHERE name IN ('Brazil', 'Russia', 'India', 'China');
SELECT name, population FROM world
  WHERE name IN ('Sweden','Norway', 'Denmark');

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


Language: English  • Deutsch • 中文