Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi
I'm in urgent need of a way to select duplicate records in two tables.

Table1 has columns A and B and has around 4000 records
Table2 has columns A and B and has around 8000 records

I want to select all records which have a duplicate value for B in both tables.

The following query should work but produces 20000 records which is obviously wildly incorrect.

SQL
select Table1.A, Table1.B FROM Table1, Table2 WHERE Table1.B = Table2.B


What I have tried:

................................................................................................................
Posted
Updated 25-Sep-17 10:06am

Use an SQL INNER JOIN[^]
 
Share this answer
 
I take it that the same value for B can be repeated multiple times. One way to check this could be to use EXISTS clause.

For example:
SQL
SELECT *
FROM Table1 t1
WHERE EXISTS (SELECT 1 
              FROM Table2 t2
              WHERE t2.B = t1.B)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900