You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CREATETABLEteacher(
id INT(10) PRIMARY KEYNOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL
);
CREATETABLEstudent(
id INT(10) PRIMARY KEYNOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL
);
CREATETABLEteacher_student (
teacher_id INT(10) NOT NULL,
student_id INT(10) NOT NULL
);
Teachers with more than 5 students
SELECT t.*FROM teacher t JOIN teacher_student ts ONts.teacher_id=t.idGROUP BYt.idHAVINGCOUNT(ts.student_id) >5;
Two first teachers with maximum mutual students
SELECTt.name, ts2.teacher_idas mutual_id, COUNT(ts1.student_id) as mutual_count
FROM teacher_student ts1
INNER JOIN
teacher_student ts2 ONts1.teacher_id!=ts2.teacher_idANDts1.student_id=ts2.student_idJOIN teacher t ONts1.teacher_id=t.idGROUP BYts1.teacher_id, ts2.teacher_idORDER BY mutual_count DESCLIMIT2;