MySQL Simple IF Function Inquiry. Appreciate the help

In MySQL mastery course under My Essential MySQL Functions

7 - IF Function practice exercise

Shouldn’t count() be count(product_id) instead of count(*) because we are counting how many products are in each order_id? or does it matter?

select
p.product_id,
name,
count(product_id) as orders,
if(count(product_id) = 1, ‘Once’, ‘Many times’) as frequency
from products p
join order_items oi using (product_id)
group by product_id

While COUNT(*) counts the number of rows COUNT(product_id) counts the number of non-null values in product_id. Since there can’t be NULLs in product_id it doesn’t really matter here.

1 Like