Setting a default value on NULL columns from LEFT JOIN and filtering using WHERE
Instead of counting the purchases in a separate query, just group the main query instead. To filter on the count, put that in a having
caluse:
select i.name, coalesce(sum(s.unit_sales), 0) as purchases_countfrom items ileft join sales s on s.item_name = i.uidgroup by i.namehaving purchases_count < 10