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

  • Home
  • SEARCH
  • 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 8921343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:31:07+00:00 2026-06-15T06:31:07+00:00

In SQLite, I want to write a SELECT query to find articles, that have

  • 0

In SQLite, I want to write a SELECT query to find articles, that have certain properties+value or that does NOT have certain properties, where properties are in another table.

This is how I’ve organized a database I’ve got:

Articles:

ID  Name
1   Paper clip
2   Water hose
3   Rubber duck

Features
ID    Articles_ID  Name    Value
1     1            Color   White
2     2            Color   Yellow
3     2            Length  1.4m
4     3            Color   Yellow

If I would like to find articles with the color Yellow I could do:

SELECT distinct a.Name from Articles a join Features f
             where a.ID = f.Articles_ID
               and f.Name = "Color"
               and f.Value = "Yellow";

But what if I would like to find articles with the color yellow, but without any Length feature. I.e. I want the rubber duck, since it has no Length, but I don’t want the water hose.

In my UI one can choose:

Color: [ ] <empty>
       [x] Yellow
       [ ] White
       [ ] ...

Length: [x] <empty>
        [ ] 0.7m
        [ ] 1.4m
        [ ] ...

My articles table has ~20k rows and features ~200k.

Perhaps my database not suitable for this kind of queries? I could easily regenerate it if necessary.

  • 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-06-15T06:31:09+00:00Added an answer on June 15, 2026 at 6:31 am

    (Note: you probably need the article IDs, in which case you don’t need DISTINCT; and the syntax for joins should be tableA JOIN tableB ON condition.)

    There are three possibilities: You can get all yellow articles, and then exclude all articles that have a length:

    SELECT a.ID,
           a.Name
    FROM Articles a
    JOIN Features f ON a.ID = f.Articles_ID
    WHERE f.Name = 'Color'
      AND f.Value = 'Yellow'
    EXCEPT
    SELECT a.ID,
           a.Name
    FROM Articles a
    JOIN Features f ON a.ID = f.Articles_ID
    WHERE f.Name = 'Length'
    

    Alternatively, use a subquery to match records for which corresponding Length records do not exist:

    SELECT a.ID,
           a.Name
    FROM Articles a
    JOIN Features f ON a.ID = f.Articles_ID
    WHERE f.Name = 'Color'
      AND f.Value = 'Yellow'
      AND NOT EXISTS (SELECT ID
                      FROM Features f2
                      WHERE f2.Articles_ID = a.ID
                        AND f2.Name = 'Length')
    

    Alternatively, use a LEFT JOIN to join with all Length features, and match those records where such a join did not succeed.
    (With the Color condition in the first feature join, this query is the most regular.)

    SELECT a.ID,
           a.Name
    FROM Articles a
    JOIN      Features f1 ON  a.ID = f1.Articles_ID
                          AND f1.Name = 'Color'
    LEFT JOIN Features f2 ON  a.ID = f2.Articles_ID
                          AND f2.Name = 'Length'
    WHERE f1.Value = 'Yellow'
      AND f2.Value IS NULL
    

    Which query is most efficient depends on what indexes you have and whether SQLite decides to use them.
    Use EXPLAIN QUERY PLAN to check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you want to write a query in Python that will select (from SQLite
I want to run a SELECT ... LIKE query in SQLite that is case-sensitive.
I want to convert sqlite query to Ormlite query. SELECT * FROM Test where
I'm trying to write a SQLite query in Android where I select all rows
I want to write SQLite statement something like this: SELECT * FROM Table WHERE
I have a sqlite table that has amount field with NUMERIC(10,5) value(I know data
I am trying to write a select query from one single table in SQLite
I have a sqlite database with questions and I want to write a function
i want to write query in sqlite for android in which i want to
I'm trying to write code that will be executed if an SQLite query won't

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.