---
source: https://iinventedlight.com/library/notes/django-aggregation.md
title: "Summarize a Django QuerySet with aggregate() or annotate()"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Django docs); Public-Domain (SQLite); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Django docs, Aggregation; SQLite, Built-in Aggregate Functions. BSD-3-Clause; Public-Domain."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: Django aggregation and SQLite aggregate functions"
tags: [django, aggregation, annotate, sqlite, orm]
client: Librarian
sources:
  - id: S1
    title: "Aggregation"
    publisher: Django docs
    url: https://docs.djangoproject.com/en/5.2/topics/db/aggregation/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "Built-in Aggregate Functions"
    publisher: SQLite
    url: https://www.sqlite.org/lang_aggfunc.html
    accessed: 2026-11-19
    license: "Public-Domain"
---
# Summarize a Django QuerySet with aggregate() or annotate()
**Answer:** Django has two common ways to run aggregate queries. `aggregate()` generates summary values over an entire `QuerySet`. `annotate()` attaches a summary to each object, such as a `Count` of related books on each publisher.

## aggregate() and annotate()

Appending an `aggregate()` clause onto the `QuerySet` calculates a value such as `Avg("price")` over the set. `all()` is redundant in that example. `annotate(num_books=Count("book"))` is the per-object form. `Avg`, `Max`, `Min`, and `Count` appear in the examples.

## What the database computes

The aggregate functions shown below are available by default in SQLite. The result of `avg()` is NULL if there are no non-NULL inputs. `Avg("price", default=0)` is one of the examples. For aggregate functions like `max()` and `count()`, the input order does not matter. `count(*)` with no arguments returns the total number of rows in the group. `sum()` returns NULL for no non-NULL input rows, but `total()` returns 0.0.

| aggregate() | annotate() |
|---|---|
| aggregate() | Summary values over an entire QuerySet |
| annotate() | Per-object summary such as Count |
| Avg | avg(); NULL if there are no non-NULL inputs |
| Count | count() over a group of rows |

## See also

- [django-querysets](https://iinventedlight.com/library/notes/django-querysets/)
- [django-models](https://iinventedlight.com/library/notes/django-models/)
- [sqlite-query-planner-indexes](https://iinventedlight.com/library/notes/sqlite-query-planner-indexes/)

## Claims

- id: C1
  claim: aggregate() generates summary values over an entire QuerySet.
  source: S1
  evidence: "generate summary values over an entire QuerySet"

- id: C2
  claim: Appending aggregate() onto a QuerySet calculates summary values such as Avg.
  source: S1
  evidence: "This is done by appending an aggregate() clause onto the QuerySet"

- id: C3
  claim: SQLite's avg() is NULL when there are no non-NULL inputs.
  source: S2
  evidence: "The result of avg() is NULL if there are no non-NULL inputs"

## Sources

- [S1] Aggregation. Django docs. https://docs.djangoproject.com/en/5.2/topics/db/aggregation/ . License: BSD-3-Clause. Accessed 2026-11-19.

- [S2] Built-in Aggregate Functions. SQLite. https://www.sqlite.org/lang_aggfunc.html . License: Public-Domain. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Django docs, Aggregation; SQLite, Built-in Aggregate Functions. BSD-3-Clause; Public-Domain. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
