Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:18:05+00:00 2026-05-30T14:18:05+00:00

I have two SQLite tables, recipes and ingredients. I need to find all of

  • 0

I have two SQLite tables, recipes and ingredients. I need to find all of the recipes which have between 2 and 4 items from a list of ingredients but I can’t get my head round the SQL to make this work.

The tables are:

CREATE TABLE recipes (
    rowidx INTEGER  AUTOINCREMENT,
    RecipeID TEXT(10) NOT NULL PRIMARY KEY,
    Name TEXT(255) NOT NULL
);

 CREATE TABLE Ingredients (
    Recipe TEXT(10) NOT NULL PRIMARY KEY,
    Ingredient TEXT(255) NOT NULL COLLATE NOCASE,
    Measurement TEXT(255) NOT NULL
);

I started with something simple, but ran out of steam when I came to the ‘between n and n ingredients’ part.

SELECT COUNT(*) FROM Recipes 
WHERE RecipeID IN (
    SELECT Recipe FROM Ingredients WHERE Ingredient IN (milk','butter','sugar','flour','egg' ) 
) 

I’m sure there must be a relatively simple way to do this but it’s not clicking.

EDIT: I actually ended up with a modified version of the answer below:

SELECT *,ifnull((SELECT COUNT(i.Ingredient) AS IngredientCount FROM Recipes r LEFT JOIN Ingredients i ON r.RecipeID = i.Recipe WHERE i.Ingredient IN ('flour' ) and r.recipeid = allrecipes.recipeid GROUP BY R.RecipeID),0) AS IngredientCount 
FROM Recipes allrecipes
WHERE IngredientCount BETWEEN 2 AND 4

Unlike the original answer, if a recipe matches no ingredients, and BETWEEN 0 AND 2 is specified, it won’t even appear in the list for sorting, so will be excluded.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:18:06+00:00Added an answer on May 30, 2026 at 2:18 pm
    SELECT
        r.Name,
        COUNT(i.Ingredient) AS Ingredients
    FROM
        recipes r
        LEFT JOIN ingredients i 
            ON i.Recipe = r.RecipeID
            AND i.Ingredient IN ('milk','butter','sugar','flour','egg')
    GROUP BY r.Name
    HAVING COUNT(i.Ingredient) BETWEEN 2 AND 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL statement (SQLite 3.0) which is joining two tables, the
I have two tables in a SQLite DB, and both have the following fields:
I have a problem. I have two sqlite files and I need (with PHP)
I have NSTableView with two columns, and i wanna fill them entries from SQLite
I have two SQLite tables, that I would love to join them on a
I have a sqlite query that I'm trying to write. I have two tables:
I need some help writing a SQL statement. I have two tables in a
I have two table views displaying the data from a Sqlite database. I can
Help a sqlite n00b out, please. I have two tables CREATE TABLE students (
I have a custom Android ContentProvider which stores and retrieves data from a SQLite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.