I am a new and am having some trouble with joining two tables.
table1: (topicID, questionID) where topicID is the primary key. For each topicID, there are many questionID’s.
Now, I have a table2 with questioIDs and I want to get all those topicID’s from table1 which have atleast one entry for each questionID in table2.
I would appreciate any help.
create table table1(
topicID int,
questionid int)
create table table2
(
questionid int
)
insert into table1
select 1,1
union all
select 2,1
union all
select 2,2
union all
select 2,4
union all
select 1,2
union all
select 1,6
insert into table2
select 1
union all
select 2
union all
select 6
Using table1 and table2 above, the query should return topicID as 2 as only this has atleast one entry for each questionID’s in table2.
Thanks
The relational operator you require is division, popularly known as “the supplier who supplies all parts”.
Because your spec states “has at least one entry for each questionID”, I think you should be looking at division with remainder.
p.s. this is not a topic you’ll find “on Page 1 of almost any SQL reference”, rather it is one of the more obscure relational operators!