Reputation: 85
Assuming i have following table
STUDENTS
--------
StudentID Course
1 1
1 2
1 3
2 1
2 2
3 1
3 3
Also, I have a certain set of important courses
IMPORTANT COURSE
----------------
Course
1
2
5
8
How can I find out which students take exclusively important courses? (Student 2 in this case)
Upvotes: 0
Views: 57
Reputation: 204904
If you group by the student you can count the number of courses that are not in the important courses
table. That count should be 0
select studentid
from students s
left join `important courses` c on c.course = s.course
group by studentid
having sum(c.course is null) = 0
Upvotes: 2