0

I have 3 mysql table subjects, exams and exams_subjects. subject contains subject ids, exam contains exam ids and exam_subjects contains subjects ids and exam ids. I want to write a mysql query to get an output of subject ids from exams_subjects table that are common to all exam ids. ie the intersection of subject ids for different exam ids. How can I write such a query ?

Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
ARUN P.S
  • 1,713
  • 2
  • 16
  • 19

1 Answers1

0
SELECT subjectid 
FROM exam_subjects 
WHERE examid IN (SELECT examid FROM exams) 
GROUP BY subjectid 
HAVING COUNT(*) = (SELECT COUNT(examid) FROM exams);

I got this with the help of a similar question in stack overflow How to do intersection on a composition table

Community
  • 1
  • 1
ARUN P.S
  • 1,713
  • 2
  • 16
  • 19