emo
emo

Reputation: 137

Querying SQL for multiple records

I have a table containing some information, and my wish is to make a select query to retrieve the wanted informations.

My table looks like,

Column1     Column2     column3     ...
Company1    Doc1
Company1    Doc2
Company1    Doc3
Company2    Doc1
Company2    Doc3
Company3    Doc1
...

So what I want is, to select all the companies containing all three documents. That is, if Compan1 contains Doc1, Doc2, Doc3, select it. If it only contains Doc2 and Doc3, or anything else, do not select it.

So overall, I want a overview of all the companies containing those exact three documents.

Hope it gives sense :).

Upvotes: 0

Views: 45

Answers (1)

user330315
user330315

Reputation:

select column1
from the_table
where column2 in ('Doc1', 'Doc2', 'Doc3')
group by column1
having count(distinct column2) = 3;

Upvotes: 3

Related Questions