When should I use COUNT(*) versus COUNT(column) in SQLite?
Quick answer
Use COUNT(*) to count all rows, including rows where answer is NULL. Use COUNT(answer) to count only rows where answer is non-NULL. In a hypothetical survey with four rows—three non-NULL answers and one NULL—the counts are 4 and 3, respectively. [S1]
The question, the details, the next step.
A practical checklist
Decide whether you are counting rows or non-NULL values in a particular column.
Use
COUNT(*)for all rows, orCOUNT(answer)for rows whereansweris non-NULL.Select both expressions together when comparing total rows with answered rows.
For counts by question, group by the question identifier and interpret each count within its group.
Label results by what they measure; check your data rules before calling answers valid or complete.
A few more useful answers
Does `COUNT(answer)` include rows where `answer` is NULL?
No. It counts occurrences where the expression is not NULL. Use COUNT(*) if you want to include every row. [S1]
Does `COUNT(*)` count all rows when I group by question?
It counts all rows within each group. Without GROUP BY, it counts across the input dataset as a whole. [S1]
Do these counts tell me how many people answered?
Not necessarily. These expressions count rows or non-NULL column values; they do not establish whether each row represents a distinct person.
Sources
References used for this guide.
- [S1] SQLite built-in aggregate functions — COUNT sqlite.org · Checked
