not in, in and common queries

Experts:

I have a following table design - object id is common between two tables,

image--6-.png
I am looking for common elements, not in table a vs table b, in table a vs table b... I have tried oracle "in" and "not in" sub queries but they are not working for me.. Is there any alternative methods?
LVL 5
CalmSoulAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
in an not in exactly like exists and not exists and also join and left join + where x not null all will work to get the results.
there are differences in the results as soon as you may have "duplicates" in either table, or NULL values in either table, and depending if you need to fetch other column data from the "other" table.

indexes on the field object_id are a "must" in any case

for 1, you can use any of those:
select a.object_id from tableA a where exists ( select null from tableB b where a.object_id in = b.object_id  );
select a.object_id from tableA a where a.object_id in ( select b.object_id from tableB b where b.object_id is not null );
select a.object_id from tableA a join tableB b on a.object_id in = b.object_id;

with the first one shall be the fastest one, the larger the tables are.
the last one may result in duplicates if either table contains duplicates


for 2, you can use any of those:
select a.object_id from tableA a where not exists ( select null from tableB b where a.object_id in = b.object_id  );
select a.object_id from tableA a where a.object_id not in ( select b.object_id from tableB b where b.object_id is not null );
select a.object_id from tableA a left join tableB b on a.object_id in = b.object_id where b.object_id is null;

here, the first and the last one shall be the fastest ones.
the last one may result in duplicates if tableA contains duplicates

for 3, you simply reverse tableA and tableB objects in the queries

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Farzad AkbarnejadDeveloperCommented:
Hi,
Place your own written queries here to help to correct it.

-FA
Guy Hengel [angelIII / a3]Billing EngineerCommented:
actually, I got 2 and 3 the wrong way round...

also there is another option, but rarely used as most often you also need other column data:

select a.object_id from tableA a
minus
select b.object_id from tableB b

will return result 2

reverse the query to get result 3
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

slightwv (䄆 Netminder) Commented:
Using the set operators like the last post, to get #1:
select a.object_id from tableA a
intersect
select b.object_id from tableB b


I actually prefer to use MINUS,UNION and INTERSECT over complicate joins.  They are pretty efficient at what they do and can often outperform joins.  You should always test in your own database.
CalmSoulAuthor Commented:
Intersect will work for 1 but how about 2 and 3?
slightwv (䄆 Netminder) Commented:
2 and 3 were in post http:#a40699948 directly above mine.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.