count(*) vs count(column-name) - which is more correct?
count, sql
Solution
- `COUNT(*)` counts all rows
- `COUNT(column)` counts non-NULLs only
- `COUNT(1)` is the same as `COUNT(*)` because 1 is a non-null expression
Your use of `COUNT(*)` or `COUNT(column)` should be based on the desired output only.
Problem
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; ```