finding common records from two tables

Posted on 2013-01-07
Last Modified: 2013-02-05
I have two tables with same columns  i have key based upon that key i want to see  if i have any dups or common records for that two tables

how can i do that.

Question by:sam2929
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38752661
Use intersect?

select key_col1, key_col2 from table1
select key_col1, key_col2 from table2

You could join them as well but I figure intersect will be faster.
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38752663
forgot the join:

select t1.keycol1, t1.keycol2 from tab1 t1, tab2 t2
where t1.keycol1=t2.keycol1 and t1.keycol2=t2.keycol2;
LVL 14

Expert Comment

ID: 38753060
You can also try out EXISTS clause.

assuming you want data only from table1 that EXISTS in table2

select t1.* from tab1 t1
(select 1 from tab2 t2
where t1.col1=t2.col1
and t1.col2=t2.col2)
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Author Comment

ID: 38753126
Now i want to delete the dups how can i do that
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38753138
Something like:
Delete from table1 where rowid in
Select t1.rowid
From ...whatever select you wanted to use

If you want to delete from table2, just switch the names.
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38753141

That deletes all rows.  If that isn't what you want, just let us know.

Author Comment

ID: 38753305
No i don't want to delete all rows

table a looks like

doc_num    yr           line
123            2010         1
234            2010         1

table b looks like
doc_num    yr           line
123            2010         1
345           2010         1

i want final result as
doc_num    yr           line
234            2010         1

table b looks like
doc_num    yr           line
123            2010         1
345           2010         1
LVL 77

Accepted Solution

slightwv (䄆 Netminder) earned 500 total points
ID: 38753339
I didn't mean ALL rows.  I just meant all 'matching' rows. That is what you showed in the results.

When trying things like this, set up a simple test case to see what happens.

Here is a complete test using your sample data.  If you do not get the expected results, please add to it and show where it fails.

drop table taba purge;
create table taba(doc_num number, yr number, line number);

drop table tabb purge;
create table tabb(doc_num number, yr number, line number);

insert into taba values(123,2010,1);
insert into taba values(234,2010,1);

insert into tabb values(123,2010,1);
insert into tabb values(345,2010,1);

delete from taba
where (doc_num,yr,line) in (select doc_num,yr,line from tabb);

select * from taba;
select * from tabb;

Open in new window


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
any step by steps guide on how to install Oracle 12c on Windows 10 8 114
Oracle dataguard 5 61
Oracle Errors 11 52
error doing substr 3 37
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question