153k views
3 votes
Find students (SSN) from the enrollment table who are enrolled in all classes. Order the result by SSN.

a) SELECT SSN FROM Enrollment WHERE COUNT(DISTINCT Class) = (SELECT COUNT(DISTINCT Class) FROM Classes);

b) SELECT SSN FROM Enrollment GROUP BY SSN HAVING COUNT(DISTINCT Class) = (SELECT COUNT(DISTINCT Class) FROM Classes);

c) SELECT SSN FROM Enrollment WHERE COUNT(Class) = (SELECT COUNT(DISTINCT Class) FROM Classes);

d) SELECT SSN FROM Enrollment WHERE COUNT(DISTINCT Class) >= (SELECT COUNT(DISTINCT Class) FROM Classes);

1 Answer

3 votes

Final answer:

The correct query to find students who are enrolled in all classes is option B.

Step-by-step explanation:

The correct answer is option B: SELECT SSN FROM Enrollment GROUP BY SSN HAVING COUNT(DISTINCT Class) = (SELECT COUNT(DISTINCT Class) FROM Classes);



This query will select all students (SSN) from the Enrollment table who are enrolled in all classes. It groups the records by SSN and filters the result by the number of distinct classes each student is enrolled in, comparing it to the total number of distinct classes in the Classes table using the HAVING clause.



This method ensures that only students who are enrolled in all classes will be returned in the result, ordered by SSN.

User Christabel
by
8.4k points