Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between COUNT(*) and COUNT(table.ColumnName)? [duplicate]

Tags:

sql

database

What is the difference between COUNT(*) and COUNT(table.ColumnName)?

I always thought that it is faster to put a column name as a parameter for COUNT, but now when I think of it I really don't know because * means that the function counts rows, so probably there isn't any difference? Or maybe giving a specific column name even slows the process?

like image 607
nelaed Avatar asked Mar 30 '26 16:03

nelaed


1 Answers

The difference between these two is not (primarily) performance. They count different things:

COUNT(*) counts the rows in your table.

COUNT(column) counts the entries in a column - ignoring null values.

Of course there will be performance differences between these two, but that is to be expected if they are doing different things. Especially when the column allows null-values, the query will take longer than on a column that does not (or COUNT(*)).

like image 168
germi Avatar answered Apr 02 '26 12:04

germi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!