Select row with more of one type than another in a separate table
I want to get all customers that have purchased more apples than pears but have bought at least one apple.


SELECT c.id
FROM customers as c
JOIN products p ON c.id = p.customer_id
WHERE p.type IN ('apple', 'pear')
GROUP BY c.id
HAVING SUM(p.type='apple') &gt; SUM(p.type='pear')