I have a diners table:
NAME | FOOD
-----------------
matthew | rice
matthew | beans
mark | rice
mark | beans
Luke | rice
john | beans
I need to extract names that have had only rice, names that have had only beans and both. A LEFT JOIN would work if they were in separate tables. But I’m having difficulty because they’re in one table.
I’ve tried variants of these 2 statements with no luck:
SELECT name
FROM diners
WHERE NOT EXISTS
(SELECT name
FROM diners
WHERE food = 'beans')
SELECT t1.name FROM diners AS t1
LEFT JOIN diners AS t2 ON t1.name = t2.name
WHERE t2.food = 'rice'
AND t2.name IS NULL
— names that have had BOTH
— names that have had only rice
–names that have had only beans