Consider the following schema:
Student(snum, sname, major,level)
Class(cname, meets-at,room, fid)
Faculty(fid, fname, depname)
Enrolled(snum, cname)
write an SQL query to find the names of all the classes that don’t meet in room R128 with the smallest number of students(compared to all other classes).
Advertisement
Answer
Try this
SELECT E.cname, Count(snum)
FROM Enrolled E
GROUP BY E.cname
HAVING Count(snum) in(
SELECT TOP 1 Count(snum) cnameCount
FROM Enrolled E
JOIN Class C
ON E.cname=C.cname
WHERE C.room!='R128'
GROUP BY c.CName
ORDER BY Count(snum)
)