Jhay
Jhay

Reputation: 15

SQL- Select and Group By

I want to have a query that will get the subject that has "cs", "cp", "is" in code column. what query will i use ?

sample table

Subject | Code
1 | cs
1 | cp
1 | is
2 | cp
2 | cs
3 | cs
3 | cp
3 | is
4 | cp

so the output must be

subject
1
3

Upvotes: 0

Views: 39

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269445

You can do this using group by and having:

select subject
from t
where code in ('cp', 'cs', 'is')
group by subject
having count(distinct code) = 3;

Upvotes: 2

Related Questions