Hello all, I need to find all the records of one table that relate to ALL records of another table. I was thinking that I can check to see if a two tables are equal (via sub-selects). Is there a way to compare tables in SQL or is there an easier way to do this?

The first thing I can think of is to do a count of both tables and see if they are the same size. Second do a left join from the ID field in table1 to the ID field in table2 and get the records in table1 having table2.ID is null:

select table1.id as t1ID, table1.name, ......., table2.id as t2ID
from table1
left join table2 on table1.id = table2.id
order by table1.ID
HAVING t2id is null

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.