Smiley
Smiley

Reputation: 107

SQL Select row from a table which has a link with every possible value that exists in a column in another Table

I have something like this two tables:

  A
 ____
| ID |   
|----|
| 1  |
| 2  |
 ----

  B
 ____ ______
| ID | TYPE |      
|----|------|   
| 1  |  Q   |   
| 2  |  W   |
| 1  |  W   |   
 ---- ------

Here i want to return 1 from table A cause it's linked with all possible types that exist in table B.

EDIT: I don't know before the query what all possible types are

Upvotes: 0

Views: 61

Answers (1)

sstan
sstan

Reputation: 36523

You can always try this:

select id
  from b
 group by id
having count(distinct type) = (select count(distinct type) from b)

Upvotes: 4

Related Questions