I have a SQL query that has me stumped. Basically, I have a Recipes table that contains (as you’ve no doubt guessed,) many recipes. I have an Ingredients table which contains all sorts of ingredients. I have a RecipeIngredients table that links a recipe to what ingredients it uses. Lastly, I have a PopularIngredients table (it’s actually a view, but who cares?) that contains the most popular ingredients people might have in their kitchen:
CREATE Table Recipes
(
RecipeId int4,
Title varchar(100)
);
CREATE Table Ingredients
(
IngredientId int4,
Name varchar(100)
);
CREATE Table RecipeIngredients
(
RecipeId int4,
IngredientId int4,
Amount int2
);
CREATE Table PopularIngredients
(
IngredientId int4
);
My goal is to get a list of all recipes that use only popular ingredients.
A SQL Fiddle with sample data can be found here.
What I’m looking for is a query that will return Chicken Salad and Pancakes. Aligator Burgers would not be returned, since it uses aligator which is not a popular ingredient.
I’ve tried a few things involving sub-selects and the ALL keyword, but haven’t had any luck. I’ve tried various inner and outer joins, but Recipe rows will still show up as long as at least one of its ingredients is popular. Any help would be much appreciated!
I’m using Postgres 9.1.
This gets all recipes which have no ingredients that are not in the PopularIngredients table.