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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:47:04+00:00 2026-05-12T05:47:04+00:00

I want to efficiently check if a table contains any rows that match <condition

  • 0

I want to efficiently check if a table contains any rows that match <condition A> and do not match <condition B>, where the conditions are arbitrary.

In Oracle, this almost works:

select count(*) from dual
where exists (
  select * from people
  where (<condition A>)
  and not (<condition B>)
);
-- returns zero if all rows that match <condition A> also match <condition B>
-- (well, almost)

The problem is the dreaded null values. Lets say <condition A> is name = ‘Aaron’ and <condition B> is age = 21. The query will correctly identify any Aarons whose age is not equal to 21, but it fails to identify any Aarons whose age is null.

Here is a correct solution, but on a table with millions of records it can take a while:

select (
  select count(*) from people
  where (<condition A>)
) - (
  select count(*) from people
  where (<condition A>)
  and (<condition B>)
) from dual;
-- returns zero if all rows that match <condition A> also match <condition B>
-- (correct, but it is s l o w...)

Unfortunately the two conditions will be arbitrary, complex, changing, and generally out of my control. They are generated from the application’s persistence framework from user searches, and while we try to keep our indexes up with our users, a lot of the time they will cause big table scans (which is why that first query with the “exists” clause is so much faster than the second – it can stop as soon as it finds one matching record, and it doesn’t have to do two separate scans).

How can I do this efficiently without futzing up on the nulls?

  • 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-12T05:47:05+00:00Added an answer on May 12, 2026 at 5:47 am

    If you do have the id field, try:

    select count(*) from dual
    where exists (
    select * from people
    where (cond a)
    and zzz.id not in (select id from people where (cond b))
    );

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

Sidebar

Related Questions

I want to check that an IEnumerable contains exactly one element. This snippet does
this c# code is probably not the most efficient but gets what I want
I have a MySQL table from a third-party application that has millions of rows
I have a huge table to work with . I want to check if
I have two tables tabData and tabDataDetail. I want all idData(PK) from Parent-Table(tabData) that
I want to use a temp directory that will be unique to this build.
I have a mysql table, I want to check if a row exists where
Suppose I want to implement a reasonably efficient 'keyword recognition algorithm', that is first
I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
I would like to check if my array has any duplicates and return the

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.