I am new to writing SQL queries and am getting stuck on some simple ones, I need to find
drinkers who only like bud
WHERE beer = 'Bud';
There are a number of ways to solve this question. I prefer to use the one with not exists subqueries because the subquery in this case does not pull the actual data therefore it is fast:
SELECT drinker FROM likes l1 WHERE beer = 'Bud' AND NOT EXISTS (SELECT 1 FROM likes l2 WHERE l2.drinker=l1.drinker and l2.beer <> "Bud");
What it pretty much does is that it selects those drinkers that like Bud and tgen checks if those like anything else.
not exists returns true if no records match the condition in the subquery.