Does it make a difference if you do count(*)
vs count(column-name)
as in these two examples?
I have a tendency to always write count(*)
because it seems to fit better in my mind with the notion of it being an aggregate function, if that makes sense.
But I'm not sure if it's technically best as I tend to see example code written without the *
more often than not.
count(*):
select customerid, count(*), sum(price) from items_ordered group by customerid having count(*) > 1;
vs. count(column-name):
SELECT customerid, count(customerid), sum(price) FROM items_ordered GROUP BY customerid HAVING count(customerid) > 1;
The simple answer is no – there is no difference at all. The COUNT(*) function counts the total rows in the table, including the NULL values.
Your use of COUNT(*) or COUNT(column) should be based on the desired output only. ... if you have a non-nullable column such as ID, then count(ID) will significantly improve performance over count(*). The two seem to contradict each other.
for all purposes count(*) and count(1) are the same, you should use count(*) though.... count (col) only counts the not null entries in the table. That's incorrect, count(*) counts all rows, no matter of null values.
The difference is simple: COUNT(*) counts the number of rows produced by the query, whereas COUNT(1) counts the number of 1 values.
COUNT(*)
counts all rowsCOUNT(column)
counts non-NULLs onlyCOUNT(1)
is the same as COUNT(*)
because 1 is a non-null expressionsYour use of COUNT(*)
or COUNT(column)
should be based on the desired output only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With