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 3611236
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:51:06+00:00 2026-05-18T21:51:06+00:00

I have a query that needs to return results that are NOT matched in

  • 0

I have a query that needs to return results that are NOT matched in a sub query. The sub query can return an empty result, so I need to set a default value (say 0) if the sub query returns an empty set to prevent IN (NULL) which always returns another NULL.

For example

SELECT * FROM example_table WHERE id NOT IN (subquery_that_selects_ids)

subquery_that_selects_ids can return a set of integers, i.e. (1,2,5,6)
or an empty set if subquery finds no matching results.

COALESCE doesn’t work here, since the sub query will likely return more than one result.

Solutions need to work in SQLite or postgresql.
How can I prevent the sub query from returning an empty set?


Everyone is telling me that the query should work as written. And you are all correct. The query is being built by Rails3’s AREL, as I was about to post the full query here I noticed that AREL was putting NULL in for an empty set when using array conditions.

I.E. My query in rails looked like:

Object.where("id NOT IN (?)", Object.where(other_conditions).select(:id))

when Object.where(other_conditions) evaluated to [] the ? was being replaced with NULL

So I re-write the query to look like:

Object.where("id NOT IN (" + Object.where(other_conditions).select(:id).to_sql + ")")

Problem solved.

I’m giving credit to @Michael Buen, but also upvoting anyone who told me the query would work as written, since they are correct. Thanks to @OMG Ponies and @Ted Elliott especially!

  • 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-18T21:51:07+00:00Added an answer on May 18, 2026 at 9:51 pm

    Try:

    SELECT * FROM example_table 
    WHERE id NOT 
        IN (select x.id from subquery_that_selects_ids as x where x.id is not null)
    

    I think you are complicating it a bit, NOT IN will have rows even there’s no rows in subquery. Your query will work without modification. Anyway, if you really desire your subquery to yield row(s) even if the conditions wasn’t satisfied, use UNION

    SELECT * FROM example_table 
    WHERE id NOT 
        IN (select x.id from subquery_that_selects_ids as x 
            where 1 = 0 -- empty set
            union
            select 0)
    

    UNION eliminates duplicate anyway, UNION ALL preserve duplicates

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

Sidebar

Related Questions

Here is the issue I am having: I have a large query that needs
I have a query that is currently using a correlated subquery to return the
I have a query that works on Postgresql 7.4 but not on Postgresql 8.3
I have a criteria query that I am using to show pages of results.
How to return a select result with batches through a single query, that should
I have a long running query that returns a large data set. This query
So I have an SQL dump file that needs to be loaded using mysql_query().
I have a query that is dynamically built after looking up a field list
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query that looks like this: public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)

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.