r/SQL • u/geminigamer369 • Jul 30 '26
Discussion Differences between counts
What is the difference between
COUNT(*)
COUNT(1)
COUNT(column_name)
8
Upvotes
r/SQL • u/geminigamer369 • Jul 30 '26
What is the difference between
COUNT(*)
COUNT(1)
COUNT(column_name)
View all comments
Show parent comment
2
u/markwdb3 When in doubt, test it out. Jul 30 '26 edited Jul 30 '26
Below is a brief demo showing that
COUNT(NOT_NULL_COLUMN)performs worse thanCOUNT(*)on MySQL. YMMV on other SQL Engines.Results were similar with repeated trials.
So while for this particular query it was only maybe a 7% difference in performance, it's definitely not NOTHING. And given the NOT NULL constraint on
ITEM, we can see thatCOUNT(ITEM)is doing additional work for nothing.Demonstration that the two queries gave identical results. (This gives us the diffs, so no output means no discrepancies.)
So again, YMMV per SQL engine, but
COUNT(*)andCOUNT(NOT_NULL_COLUMN)are logically the same, with the latter potentially performing worse. (And as I said in parent comment, ifNOT_NULL_COLUMNcan potentially be transformed to NULL by the query, then sure, they may be logically distinct.)