count(*) vs count(column-name) - which is more correct? [duplicate] count(*) vs count(column-name) - which is more correct? [duplicate] sql sql

count(*) vs count(column-name) - which is more correct? [duplicate]


Your use of COUNT(*) or COUNT(column) should be based on the desired output only.


This applies to MySQL. I'm not sure about the others.

The difference is:

  • COUNT(*) will count the number of records.
  • COUNT(column_name) will count the number of records where column_name is not null.

Therefore COUNT(*) is what you should use. If you're using MyISAM and there is no WHERE clause, then the optimiser doesn't even have to look at the table, since the number of rows is already cached.


When it's an identifier (and guaranteed to be non-NULL) then it probably doesn't matter.

However, there is a difference between COUNT(*) and COUNT(column) in general, in that COUNT(column) will return a count of the non-NULL values in the column. There is also the COUNT(DISTINCT column) variant which returns the number of unique, non-NULL values.