Manca una tabella che associ materie e studenti (punto 2):
// student [student_id] takes subject [subject_id]
takes(student_id, subject_id)
Nota che ogni tabella di base ha un predicato caratteristico associato, parametrizzato sui nomi delle colonne: nella tabella finiscono le righe che rendono vero quel predicato. La definizione della tabella è in effetti una scorciatoia per il predicato.
// teacher [id] named [name] with email [email] teaches subject [subject_id]
teacher(id, name, email, subject_id)
// subject [id] named [name] is [description]
subject(id, name, description)
// student [id] named [name] lives at [location]
student(id, name, location)
// batch [id] at venue [venue] was taught by teacher [teacher_id] on date [date]
batch(id, venue, teacher_id, date)
// student-batch [id] reports student [student_id] being in batch [batch_id]
student-batch(id, student_id, batch_id)
// CHECK student [student_id] takes the subject taught by the teacher of batch [batch_id]
Poiché il vincolo è la parte difficile, conviene derivarlo ragionando per predicati. In SQL: sostituisci le tabelle con i nomi, l'AND con JOIN/ON/WHERE e il FOR SOME con SELECT.
SELECT t.student_id AS student_id, b.id AS batch_id
FROM takes k
JOIN teacher t ON k.subject_id = t.subject_id
JOIN batch b ON t.id = b.teacher_id
WHERE k.student_id = t.student_id;
Il vincolo "ogni riga di student-batch deve soddisfare il predicato" si traduce in NOT EXISTS (SELECT ... EXCEPT ...), che però MySQL non supporta: puoi usare un trigger che verifichi EXISTS(query) dopo l'inserimento, oppure aggiungere colonne e vincoli di chiave esterna compositi.
Per la query finale, aggiungi i JOIN con student e student-batch:
SELECT b.id AS batch, t.name AS teacher, s.name AS student
FROM takes k
JOIN teacher t ON k.subject_id = t.subject_id
JOIN batch b ON t.id = b.teacher_id
JOIN student s ON s.id = k.student_id
JOIN student-batch sb ON sb.student_id = k.student_id AND sb.batch_id = b.id
WHERE b.date = @date;