I have a SQL query that puzzles me. Basically, I have a Recipes table containing (as you no doubt guessed) many recipes. I have an Ingredients table containing all kinds of ingredients. I have a RecipeIngredients table that associates a recipe with the ingredients that it uses. Finally, I have a table PopularIngredients (actually this view, but who cares?), Which contains the most popular ingredients that people can 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.
The SQL script with sample data can be found here .
What I'm looking for is a request that will return chicken salad and pancakes. Aligator Burgers will not be returned, as it uses aligator, which is not a popular ingredient.
I tried a few things related to subsamples and the keyword ALL , but they were out of luck. I have tried various internal and external connections, but the recipe lines will be displayed until at least one of its ingredients is popular. Any help would be greatly appreciated!
I am using Postgres 9.1.
sql postgresql
Mike christensen
source share