I'm trying to find the names of all classes that either meet in room R128 or have five or more students enrolled in these two tables called 'enroll' and 'class'. I can find the two parts of this question individually, but I don't understand how I can find them both in one query.
This gives me the classes in room R128 that I want:
SELECT class.cname
FROM enroll RIGHT JOIN class ON enroll.cname=class.cname
WHERE room='R128';
and this gives me the classes with 5 or more students enrolled in them:
SELECT class.cname
FROM enroll RIGHT JOIN class ON enroll.cname=class.cname
GROUP BY enroll.cname
HAVING COUNT(enroll.cname)>4;
I can't figure out how to combine them into one query. This is what I have so far, but it returns an empty set:
SELECT class.cname, COUNT(enroll.cname)
FROM enroll RIGHT JOIN class ON enroll.cname=class.cname
WHERE room='R128'
GROUP BY enroll.cname
HAVING COUNT(enroll.cname)>4;
Here are the class table and the enroll table
The classes in room R128 are Patent Law, Data Structures, Archaeology of the Incas, Dairy Herd Management, and Introduction to Math. The classes with 5 or more students are Database Systems and Operating System Design.
Assuming that room
is in class
, I would suggest union
:
select c.cname
from class c
where room = 'R128'
union -- on purpose to remove duplicates
select e.cname
from enroll e
group by e.cname
having count(*) >= 5;