# Difference between revisions of "SUM and COUNT"

Line 1: | Line 1: | ||

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

Line 103: | Line 103: | ||

</source> | </source> | ||

</div> | </div> | ||

+ | |||

+ | <h2>What next</h2> | ||

+ | <p>[[The nobel table can be used to practice more SUM and COUNT functions.]]</p> | ||

+ | <p>[http://sqlzoo.net/w/index.php/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 09:22, 11 July 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.

## Exercises

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

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(region)
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')
```

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

## What next

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