Difference between revisions of "SUM and COUNT"

From SQLZOO
Jump to: navigation, search
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
<h3>BBC Country Profile: Aggregate functions</h3>
 
<h3>BBC Country Profile: Aggregate functions</h3>
 
This tutorial is about aggregate functions such as COUNT, SUM and AVG. An aggregate function takes many values and delivers just one value. For example the function SUM would aggregate the values 2, 4 and 5 to deliver the single value 11.
 
This tutorial is about aggregate functions such as COUNT, SUM and AVG. An aggregate function takes many values and delivers just one value. For example the function SUM would aggregate the values 2, 4 and 5 to deliver the single value 11.
 +
<div class='ref_section'>
 +
<table class='db_ref'>
 +
<caption>bbc</caption>
 +
<tr>
 +
<th>name</th>
 +
<th>region</th>
 +
<th>area</th>
 +
<th>population</th>
 +
<th>gdp</th>
 +
</tr>
 +
<tr>
 +
<td>Afghanistan</td>
 +
<td>South Asia</td>
 +
<td align='right'>652225</td>
 +
<td align='right'>26000000</td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td>Albania</td>
 +
<td>Europe</td>
 +
<td align='right'>28728</td>
 +
<td align='right'>3200000</td>
 +
<td align='right'>6656000000</td>
 +
</tr>
 +
<tr>
 +
<td>Algeria</td>
 +
<td>Middle East</td>
 +
<td align='right'>2400000</td>
 +
<td align='right'>32900000</td>
 +
<td align='right'>75012000000</td>
 +
</tr>
 +
<tr>
 +
<td>Andorra</td>
 +
<td>Europe</td>
 +
<td align='right'>468</td>
 +
<td align='right'>64000</td>
 +
<td></td>
 +
</tr>
 +
<tr>
 +
<td colspan='5'>...</td>
 +
</tr>
 +
</table>
 +
</div>
  
 
<h2>Exercises</h2>
 
<h2>Exercises</h2>
 
<p>[[Using SUM, Count, MAX, DISTINCT and ORDER BY]].</p>
 
<p>[[Using SUM, Count, MAX, DISTINCT and ORDER BY]].</p>
  
<div class="summary">Summary</div>
+
<div class="progress_panel"><div>
 
+
  <div class="summary">Summary</div>
<div class="progressbarbg">
+
  <div class="progressbarbg">
  <div class="progressbar"></div>
+
    <div class="progressbar"></div>
</div>
+
  </div>
 +
</div></div>
  
 
<div class='qu'>
 
<div class='qu'>
Show the total '''population''' of the world.                                    
+
Show the total '''population''' of the world.
 +
 
 +
                               
 
  bbc('''name''', '''region''', '''area''', '''population''', '''gdp''')
 
  bbc('''name''', '''region''', '''area''', '''population''', '''gdp''')
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
Line 54: Line 100:
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT COUNT(region)
+
SELECT COUNT(name)
 
FROM bbc
 
FROM bbc
 
WHERE area >= 1000000
 
WHERE area >= 1000000
Line 113: Line 159:
 
<div class="lsclear">Clear your results</div>
 
<div class="lsclear">Clear your results</div>
  
<h2>What next</h2>
+
<p><div class="quizlink">[[SUM and COUNT Quiz]]</div></p>
<p>[[SUM and COUNT Quiz]]</p>
+
 
<p>[[The nobel table can be used to practice more SUM and COUNT functions.]]</p>
 
<p>[[The nobel table can be used to practice more SUM and COUNT functions.]]</p>
 
<p>[[The_JOIN_operation |The next tutorial is looks at the Table Tennis database. It shows how queries may use records from two related tables.]]
 
<p>[[The_JOIN_operation |The next tutorial is looks at the Table Tennis database. It shows how queries may use records from two related tables.]]

Revision as of 12:54, 15 August 2012

BBC Country Profile: Aggregate functions

This tutorial is about aggregate functions such as COUNT, SUM and AVG. An aggregate function takes many values and delivers just one value. For example the function SUM would aggregate the values 2, 4 and 5 to deliver the single value 11.

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

Exercises

Using SUM, Count, MAX, DISTINCT and ORDER BY.

Summary

Show the total population of the world.


bbc(name, region, area, population, gdp)
SELECT SUM(population)
FROM bbc
SELECT SUM(population)
FROM bbc

List all the regions - just once each.

 
SELECT DISTINCT(region)
FROM bbc

Give the total GDP of Africa

 
SELECT SUM(gdp)
FROM bbc
WHERE region = 'Africa'

How many countries have an area of at least 1000000

 
SELECT COUNT(name)
FROM bbc
WHERE area >= 1000000

What is the total population of ('France','Germany','Spain')

 
SELECT SUM(population)
FROM bbc
WHERE name IN('France','Germany','Spain')

Using GROUP BY and HAVING.

For each region show the region and number of countries.

 
SELECT region, COUNT(name)
FROM bbc
GROUP BY(region)

For each region show the region and number of countries with populations of at least 10 million.

 
SELECT region, COUNT(name)
FROM bbc
WHERE population >= 10000000
GROUP BY(region)

List the regions with total populations of at least 100 million.

 
SELECT region
FROM bbc
GROUP BY region
HAVING SUM(population)>= 100000000
Clear your results

The nobel table can be used to practice more SUM and COUNT functions.

The next tutorial is looks at the Table Tennis database. It shows how queries may use records from two related tables.

Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense