Count(*) vs Count(Column)

Difference between count(column) and count(*)


count(*) would return an extra row in the result that contains a null and the count of null values in the column.


for eg.

create table #demo(id int,id2 int)
insert #demo values(null,null)
insert #demo values(1,null)
insert #demo values(null,1)
insert #demo values(1,null)
insert #demo values(null,1)
insert #demo values(1,null)
insert #demo values(null,null)

select count(*),count(id),count(id2) from #demo

results 7 3 2

Another minor difference, between using * and a specific column, is that in the column case you can add the keyword DISTINCT, and restrict the count to distinct values

No comments:

Post a Comment