How does COUNT(*) differ from COUNT(column) and COUNT(DISTINCT column)?
Last updated
All three count rows, but they differ in what they include: COUNT(*) counts every row, COUNT(column) skips NULLs, and COUNT(DISTINCT column) counts unique non-NULL values.
COUNT(*)counts all rows:Includes rows with NULLs; it never inspects column values, just row existence.
Optimizers often satisfy it from an index or table metadata.
COUNT(column)counts non-NULL values in that column: Rows where the column is NULL are excluded, so it can be less thanCOUNT(*).COUNT(DISTINCT column)counts unique non-NULL values:Duplicates collapse to one; NULLs still ignored.
Usually the most expensive: requires deduplication (sort or hash).
- https://www.youtube.com/watch?v=PLIWFAs67gE
- https://blog.jooq.org/counting-distinct-records-in-sql/
- https://oneuptime.com/blog/post/2026-03-31-mysql-count-star-vs-count-column/view
- https://dev.to/deko39/countcolumn-vs-count-in-sql-with-inner-left-right-full-join-explained-1a1f
- https://medium.com/@anurag.ydv36/when-to-use-count-vs-count-1-in-sql-queries-0cf1171a8273