Difference between revisions of "Using GROUP BY and HAVING."

From SQLZOO
Jump to: navigation, search
(Created page with "<h3>BBC Country Profile</h3> <p><code>GROUP BY</code> and <code>HAVING</code> <p>By including a <code>GROUP BY</code> clause functions such as <code>SUM</code> and <code>CO...")
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<h3>BBC Country Profile</h3>
+
<h3>World Country Profile</h3>
  
<p><code>GROUP BY</code> and <code>HAVING</code>
+
<code>GROUP BY</code> and <code>HAVING</code>
<p>By including a <code>GROUP BY</code> clause functions such as
+
 
  <code>SUM</code> and <code>COUNT</code>
+
By including a <code>GROUP BY</code> clause functions such as
  are applied to groups of items sharing values. When you specify  
+
<code>SUM</code> and <code>COUNT</code>
  <code>GROUP BY region </code>  
+
are applied to groups of items sharing values. When you specify  
  the result is that you get only one row for each different value
+
<code>GROUP BY continent</code>  
  of <code>region</code>  
+
the result is that you get only one row for each different value
  . All the other columns must be "aggregated" by one of <code>SUM</code>,
+
of <code>continent</code>. All the other columns must be "aggregated" by one of <code>SUM</code>,
  <code>COUNT</code> ...</p>
+
<code>COUNT</code> ...
<p>The <code>HAVING</code> clause allows use to filter the groups which are
+
 
  displayed. The <code>WHERE</code> clause filters rows before the aggregation,
+
The <code>HAVING</code> clause allows use to filter the groups which are
  the <code>HAVING</code> clause filters after the aggregation.</p>
+
displayed. The <code>WHERE</code> clause filters rows before the aggregation,
  <p>If a <code>ORDER BY</code> clause is included we can refer to columns by
+
the <code>HAVING</code> clause filters after the aggregation.
  their position.</p>
+
 
 +
If a <code>ORDER BY</code> clause is included we can refer to columns by
 +
their position.
  
 
<div class='qu'>
 
<div class='qu'>
For each region show the number of countries:
+
For each continent show the number of countries:
  bbc('''name''', region, area, population, gdp)
+
world('''name''', continent, area, population, gdp)
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT region, COUNT(name)
+
SELECT continent, COUNT(name)
   FROM bbc
+
   FROM world
  GROUP BY region
+
  GROUP BY continent
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT region, COUNT(name)
+
SELECT continent, COUNT(name)
   FROM bbc
+
   FROM world
  GROUP BY region
+
  GROUP BY continent
 
</source>
 
</source>
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
For each region show the total population:
+
For each continent show the total population:
  bbc('''name''', region, area, population, gdp)
+
world('''name''', continent, area, population, gdp)
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT region, SUM(population)
+
SELECT continent, SUM(population)
   FROM bbc
+
   FROM world
  GROUP BY region
+
  GROUP BY continent
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT region, SUM(population)
+
SELECT continent, SUM(population)
   FROM bbc
+
   FROM world
  GROUP BY region
+
  GROUP BY continent
 
</source>
 
</source>
 
</div>
 
</div>
Line 51: Line 53:
 
WHERE and GROUP BY
 
WHERE and GROUP BY
 
The WHERE filter takes place before the aggregating function.
 
The WHERE filter takes place before the aggregating function.
For each relevant region show the number of countries that has a population
+
For each relevant continent show the number of countries that has a population
 
of at least 200000000.
 
of at least 200000000.
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT region, COUNT(name)
+
SELECT continent, COUNT(name)
   FROM bbc
+
   FROM world
 
  WHERE population>200000000
 
  WHERE population>200000000
  GROUP BY region
+
  GROUP BY continent
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT region, COUNT(name)
+
SELECT continent, COUNT(name)
   FROM bbc
+
   FROM world
 
  WHERE population>200000000
 
  WHERE population>200000000
  GROUP BY region
+
  GROUP BY continent
 
</source>
 
</source>
 
</div>
 
</div>
Line 72: Line 74:
 
The HAVING clause is tested after the GROUP BY. You can test the aggregated
 
The HAVING clause is tested after the GROUP BY. You can test the aggregated
 
values with a HAVING clause.
 
values with a HAVING clause.
Show the total population of those regions with a total population of at
+
Show the total population of those continents with a total population of at
 
least half a billion.
 
least half a billion.
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT region, SUM(population)
+
SELECT continent, SUM(population)
   FROM bbc
+
   FROM world
  GROUP BY region
+
  GROUP BY continent
 
HAVING SUM(population)>500000000
 
HAVING SUM(population)>500000000
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT region, SUM(population)
+
SELECT continent, SUM(population)
   FROM bbc
+
   FROM world
  GROUP BY region
+
  GROUP BY continent
 
HAVING SUM(population)>500000000
 
HAVING SUM(population)>500000000
 
</source>
 
</source>

Revision as of 00:18, 9 December 2012

World Country Profile

GROUP BY and HAVING

By including a GROUP BY clause functions such as SUM and COUNT are applied to groups of items sharing values. When you specify GROUP BY continent the result is that you get only one row for each different value of continent. All the other columns must be "aggregated" by one of SUM, COUNT ...

The HAVING clause allows use to filter the groups which are displayed. The WHERE clause filters rows before the aggregation, the HAVING clause filters after the aggregation.

If a ORDER BY clause is included we can refer to columns by their position.

For each continent show the number of countries:

world(name, continent, area, population, gdp)
SELECT continent, COUNT(name)
  FROM world
 GROUP BY continent
SELECT continent, COUNT(name)
  FROM world
 GROUP BY continent

For each continent show the total population:

world(name, continent, area, population, gdp)
SELECT continent, SUM(population)
  FROM world
 GROUP BY continent
SELECT continent, SUM(population)
  FROM world
 GROUP BY continent

WHERE and GROUP BY The WHERE filter takes place before the aggregating function. For each relevant continent show the number of countries that has a population of at least 200000000.

SELECT continent, COUNT(name)
  FROM world
 WHERE population>200000000
 GROUP BY continent
SELECT continent, COUNT(name)
  FROM world
 WHERE population>200000000
 GROUP BY continent

GROUP BY and HAVING The HAVING clause is tested after the GROUP BY. You can test the aggregated values with a HAVING clause. Show the total population of those continents with a total population of at least half a billion.

SELECT continent, SUM(population)
  FROM world
 GROUP BY continent
HAVING SUM(population)>500000000
SELECT continent, SUM(population)
  FROM world
 GROUP BY continent
HAVING SUM(population)>500000000
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense