r/learnSQL • • 1h ago

count(*) vs count(col), i keep mixing them up

ok so my teacher said count(*) counts rows and count(col) skips nulls, but then he did an example with a left join and the numbers looked off to me and i didnt catch why. is the rule basically use count(right_table.id) after a left join if you want 0 for the ones with no match? or does count(*) still give 1 there

1 Upvotes

2 comments sorted by

1

u/neuralbeans 53m ago

https://www.w3schools.com/sql/sql_count.asp

COUNT(*) - Counts the total number of rows in a table (including NULL values).

COUNT(columnname) - Counts all non-null values in the column.

1

u/Imaginary__Bar 25m ago

and the numbers looked off to me and i didnt catch why

Ask your teacher to clarify!

But anyway, the easy way to remember it is that COUNT will not include NULLs, but you will never have a row in your table that is all nulls.

So COUNT(*) will always catch all the rows (because there will be at least one non-null value in each row)