This problem is usually termed as Relational Division
. What the query below does is it gets all the professor's name which teaches the courses such as m1
and m2
. After the professors has been filtered it then counts the number of distinct CourseName
. Obviously, it will return one if it only teaches one course :D
.
SELECT a.EmpID, a.Name
FROM Professor a
INNER JOIN Works b
ON a.EmpID = b.EmpID
INNER JOIN Course c
ON b.CourseID = c.CourseID
WHERE c.CourseName IN ('m1', 'm2')
GROUP BY a.EmpID, a.Name
HAVING COUNT(DISTINCT c.CourseName) = 1