37

Is there a difference between the following queries, assuming there is a primary field 'id' in the table (as in speed, etc)?

SELECT COUNT(id) 
  FROM table

vs.

SELECT COUNT(*) 
  FROM table
2

2 Answers 2

2

One important different is that Count(*) and Count($col_name) can show different outputs if the $col_name column is nullable, since null values don't get counted by Count.

1
  • 2
    I think the question refers to the PK when it says "id", so NOT NULL is assumed, as well as a PRI index.
    – user832146
    Commented May 12, 2019 at 9:41
2

I know this is several years old but I don't see any evidence on which one to use, so I will post here my findings.

Executing explain in MySql Workbench for an InnoDB table on MySql 5.7 I see the following:

Executing count(*)

Executing count(id)

As you can see, both results are identical, so for this scenario both expressions are equivalent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.