Totals per group with GROUP BY
Get one total per genre or author instead of one for everything.
GROUP BY makes a pile per value; each total is worked out per pile.
Show each author and their books' pages added up, in a column called total_pages.
| book_id | title | author | genre | year | pages |
|---|---|---|---|---|---|
| 1 | A Study in Scarlet | Arthur Conan Doyle | Mystery | 1887 | 108 |
| 2 | The Hound of the Baskervilles | Arthur Conan Doyle | Mystery | 1902 | 256 |
| 3 | The Da Vinci Code | Dan Brown | Thriller | 2003 | 454 |
| 4 | Charlie and the Chocolate Factory | Roald Dahl | Children | 1964 | 155 |
| 5 | Matilda | Roald Dahl | Children | 1988 | 240 |
| 6 | Dracula | Bram Stoker | Horror | 1897 | 418 |
| 7 | Emma | Jane Austen | Romance | 1815 | 474 |
| 8 | Pride and Prejudice | Jane Austen | Romance | 1813 | 432 |
| 9 | Frankenstein | Mary Shelley | Horror | 1818 | 280 |
| 10 | Harry Potter and the Philosopher's Stone | J.K. Rowling | Fantasy | 1997 | 223 |
Run your query to see the result table.
Press the green Run button or Ctrl+Enter.
How GROUP BY 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 |
piles (GROUP BY genre)
result
| genre | books |
|---|---|
| Science Fiction | 3 |
| Horror | 3 |
| Romance | 2 |
SELECT genre, COUNT(*) AS books FROM books GROUP BY genre;
One pile per genre, and COUNT(*) is worked out for each pile.
What does
GROUP BYdo?SELECT genre,COUNT(*) FROM books GROUP BY genre; It sorts the rows into groups with the same genre, then works out
COUNT,SUMorAVGonce per group. One row comes back per group.What goes in the
SELECT?The column you group by, so you can tell the groups apart, plus totals like
COUNT(*). Any other column has no single value per group.Can I group by two columns?
Yes:
GROUP BY genre, authorgives one row per genre and author pair.Where does
GROUP BYgo?After
FROMandWHERE, beforeORDER BY.WHEREpicks rows first, then the rest are grouped.How is this like Excel?
It's a PivotTable with genre in Rows and Count in Values, or
COUNTIFnext to aUNIQUElist. In new Excel, GROUPBY does it in one formula.
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) | Allowed | title comes from one row of each group, with no way to say which. Easy to get wrong without noticing. |
| PostgreSQL | Error | Every column must be in GROUP BY or inside a function like COUNT. |
| MySQL / MariaDB | Error | With ONLY_FULL_GROUP_BY, the default since MySQL 5.7. Older setups allow it, as SQLite does. |
| SQL Server | Error | Every column must be in GROUP BY or inside a function like COUNT. |
| Oracle | Error | ORA-00979: not a GROUP BY expression. |
| Sybase ASE | Allowed | A Transact-SQL extension: the groups are repeated for every row, which rarely helps. |
Know someone fighting with a spreadsheet? Send them this page.