COUNT, SUM and AVG
Get one total for the whole table: a count, a sum or an average.
COUNT, SUM and AVG turn many rows into one.
Show the average number of pages, as avg_pages, and the longest book's pages, as max_pages.
| book_id | title | author | genre | year | pages |
|---|---|---|---|---|---|
| 1 | Emma | Jane Austen | Romance | 1815 | 474 |
| 2 | Charlotte's Web | E.B. White | Children | 1952 | 184 |
| 3 | Carrie | Stephen King | Horror | 1974 | 200 |
| 4 | The Great Gatsby | F. Scott Fitzgerald | Fiction | 1925 | 180 |
| 5 | Mockingjay | Suzanne Collins | Science Fiction | 2010 | 390 |
| 6 | Dracula | Bram Stoker | Horror | 1897 | 418 |
| 7 | The Time Machine | H.G. Wells | Science Fiction | 1895 | 118 |
| 8 | Pride and Prejudice | Jane Austen | Romance | 1813 | 432 |
| 9 | Angels & Demons | Dan Brown | Thriller | 2000 | 616 |
| 10 | The Fault in Our Stars | John Green | Romance | 2012 | 313 |
| 11 | The Fellowship of the Ring | J.R.R. Tolkien | Fantasy | 1954 | 423 |
Run your query to see the result table.
Press the green Run button or Ctrl+Enter.
How COUNT, SUM, AVG works
Rows into piles, piles into rows
books
| title | genre |
|---|---|
| Dune | Science Fiction |
| Dracula | Horror |
| Emma | Romance |
| 1984 | Science Fiction |
| Carrie | Horror |
| Mockingjay | Science Fiction |
| Pride and Prejudice | Romance |
| Frankenstein | Horror |
one pile
result
| books |
|---|
| 8 |
SELECT COUNT(*) AS books FROM books;
No GROUP BY: every row lands in one pile, so one row comes back.
What are aggregate functions?
Functions that turn a whole column into one value:
COUNT,SUM,AVG,MINandMAX. The result has one row instead of one per book.What's the difference between
COUNT(*)andCOUNT(series)?COUNT(*)counts rows.COUNT(series)counts the values in a column and skips NULLs. The two differ whenever the column has empty values.Do
SUMandAVGskip NULLs too?Yes.
AVG(pages)divides by the number of books that have pages, not by every row. The table below shows a catch withAVGin some databases.How do I round an average?
ROUND(AVG(pages), 1)rounds to 1 decimal place, like Excel'sROUND.How is this like Excel?
COUNT(*)isROWSon the table (every row),COUNT(column)isCOUNTAon that column,SUM(pages)is=SUM(F2:F11), andAVG,MINandMAXareAVERAGE,MINandMAX.
How other databases do it
The lessons run on SQLite. Most of SQL is the same everywhere; these are the places where this lesson's SQL differs.
| Database | How | Note |
|---|---|---|
| SQLite (these lessons) | 236.5 | AVG always gives a decimal. |
| PostgreSQL | 236.5000000000000000 | A decimal (numeric). ROUND(AVG(pages), 1) tidies it. |
| MySQL / MariaDB | 236.5000 | A decimal with 4 places. |
| SQL Server | 236 | Whole-number columns give a whole-number average, cut off, not rounded. Use AVG(pages * 1.0). |
| Oracle | 236.5 | A number with as many decimals as it needs. |
| Sybase ASE | 236 | Whole-number columns give a whole-number average, as in SQL Server. Use AVG(pages * 1.0). |
Know someone fighting with a spreadsheet? Send them this page.