One number from many rows
After this: Count, add up, average and find the smallest and largest value of a column, for the whole table.
On this page · 5 sections · 3 checks
The head teacher asks three questions. How many enrolments are there? What is the highest mark? What is the average mark? In the last tutorial you learned to ask for rows. You could ask for every mark and then count and add them up by hand. With 10 rows that works. With 10,000 rows it does not, and it is the kind of work a computer should do.
This tutorial teaches the database to do the counting for you. The answer to "how many" is not a list of rows. It is one number.
Counting rows
Here is the smallest example. The students table has six rows.
SELECT COUNT(*)FROM students;| COUNT(*) |
|---|
| 6 |
COUNT(*) counts the rows. The star means "whole rows": it does not look at any one column. Six rows went in, and one row with one number came out.
COUNT is one of five functions that read many rows and give back one value. SQL calls them . To aggregate means to gather many things into one.
The five functions
Each one takes a column in its brackets, except COUNT(*), which counts whole rows. Here they are on the mark column of enrolments. The ten marks are 72, 65, 80, 58, 90, 85, NULL, 47, 76 and 91. One mark is NULL: Chloe's History has not been marked yet. Lesson 2 looks at that row closely. For now, know that SUM, AVG, MIN and MAX skip it, while COUNT(*) still counts its row.
| Function | What it gives | On mark |
|---|---|---|
COUNT(*) | how many rows | 10 |
SUM(mark) | the values added up | 664 |
AVG(mark) | the average: the sum divided by how many | 73.77777777777777 |
MIN(mark) | the smallest value | 47 |
MAX(mark) | the largest value | 91 |
The average has many decimal places. ROUND(AVG(mark), 1) rounds it to one place: 73.8. ROUND is not an aggregate function. It works on the one value that AVG gives back.
SELECT AVG(mark), ROUND(AVG(mark), 1)FROM enrolments;| AVG(mark) | ROUND(AVG(mark), 1) |
|---|---|
| 73.77777777777777 | 73.8 |
Watch it work
The card below runs four of the functions in one query. Press Run it and step through. FROM brings in the ten rows of enrolments. Then SELECT reads all of them and gives back a single row. The note on that step mentions GROUP BY, which lesson 3 teaches. Without it, the whole table counts as one group.
SELECT COUNT(*), SUM(mark), MIN(mark), MAX(mark)FROM enrolments;enrolments · 10 rows
| student_id | course_id | mark |
|---|---|---|
| 1 | 1 | 72 |
| 1 | 2 | 65 |
| 1 | 3 | 80 |
| 2 | 1 | 58 |
| 2 | 4 | 90 |
| 3 | 1 | 85 |
| 3 | 3 | NULL |
| 4 | 2 | 47 |
| 4 | 4 | 76 |
| 6 | 1 | 91 |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
Here is the same query to run. It is real SQLite, running in this page.
Change it and run again. Put AVG(mark) in place of SUM(mark). Then try MIN(name), MAX(name) on the students table. MIN and MAX work on text too, in alphabetical order, and give Ana and Femi.
SELECT MIN(name), MAX(name)FROM students;| MIN(name) | MAX(name) |
|---|---|
| Ana | Femi |
With WHERE
An aggregate function works on the rows that are left after WHERE. So "how many marks are 60 or more?" is a WHERE that keeps those rows and a COUNT that counts them.
SELECT COUNT(*) AS passedFROM enrolmentsWHERE mark >= 60;| passed |
|---|
| 7 |
Work through it:
- FROM brings in all 10 enrolments.
- WHERE keeps the rows with a mark of 60 or more. The marks 58 and 47 fail. The NULL mark fails too, because a missing mark is not 60 or more. Seven rows are left.
- SELECT counts them: 7.
AS passed gives the result column a name, as you did in the last tutorial. Without it, SQLite calls the column COUNT(*).
The rule: a query whose SELECT holds only aggregate functions gives back exactly one row, however many rows go in.
Check yourself
The students table has 6 rows and 4 columns. What does this query give back?
SELECT COUNT(*)FROM students;The marks are 72, 65, 80, 58, 90, 85, NULL, 47, 76 and 91. What does this query give back?
SELECT MIN(mark), MAX(mark)FROM enrolments;The enrolments table has 10 rows. How many rows does this query give back?
SELECT SUM(mark), AVG(mark)FROM enrolments;- Count the rows of a table with COUNT(*), and name the result column with AS.
- Add up, average, and find the smallest and largest value of a column with SUM, AVG, MIN and MAX.
- Round an average to one decimal place with ROUND(AVG(mark), 1).
- Say that a query whose SELECT holds only aggregate functions gives back exactly one row, and that they work on the rows WHERE leaves.