Check duplicates from two queries in SQL

Posted on 2012-08-28
Last Modified: 2012-09-20
We have two tables in the database.


Only new data resides in the A and subsequently go to B after processing.. We want to run a query to check for duplicates between A and B. The odd part is, there is a field named "C:" we need to take out of the script as that field will never be a duplicate value
Question by:NCollinsBBP
    LVL 142

    Accepted Solution

    something like this:
    select * from A
    where exists(select null from B
       where B.col1 = A.col1
          and B.col2 = A.col2 
         ... etc ...

    Open in new window

    LVL 9

    Expert Comment

    If you are using the sql 2005 the you can use intersect it will Returns distinct values by comparing the results of two queries.

    Please refer the link more about intersect


    create table #one(a int,b int, c int)
    create table #one2(a int,b int, c int,D INT)
    insert into #one values (1,1,1)
    insert into #one values (4,2,3)
    insert into #one2 values (1,1,1,6)
    insert into #one2 values (2,2,3,4)
    insert into #one2 values (3,3,3,8)
    insert into #one2 values (4,4,4,0)
    insert into #one2 values (5,5,5,7)
    SELECT A,B,C from #one intersect select A,B,C from #one2

    Open in new window


    Author Closing Comment

    This gave me exactly what I needed.  Sorry for the delayed response, as well as for your assistance.


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now