Your design is wrong. Students do not have a class attribute; you need
enrollment for many classes.
CREATE TABLE Students
(stud_id INTEGER NOT NULL PRIMARY KEY,
...);
CREATE TABLE Classes
(class_id INTEGER NOT NULL PRIMARY KEY,
room_size INTEGER NOT NULL,
..);
CREATE TABLE Enrollment
(stud_id INTEGER NOT NULL,
class_id INTEGER NOT NULL,
PRIMARY KEY (stud_id, class_id),
...);
Quote:
count the number of students who registered for course X, subtract
that from the max number of students in the Classes table,
|
SELECT E1.class_id,
(SELECT room_size
FROM Classes AS C1
WHERE C1.class_id = E1.class_id)
- COUNT(E1.student_id) AS available_seats
FROM Enrollment AS E1
GROUP BY E1.class_id;