In my MySQL database, I have three tables students
, classes
, courses
.
One class
has many students
.
One class
has many courses
.
The courses
table has one boolean field active
, and a string field name
.
The students
table has id
field.
So overall relationship is (sorry I am not sure how to better illustrate the relationship if it is not clear) :
students (many_to_one) classes (one_to_many) courses
I would like to write a SQL query to get the ids of students
whose courses
(via class
) are within names ('foo', 'bar','etc')
and are active
.
How to write this SQL ?
I tried :
select stu.id
from students stu
inner join classes clz ON clz.id = stu.class_id
inner join courses cour ON cour.class_id = clz.id
where cour.active=true
and cour.name in ('foo', 'bar', 'etc');
Is it fine?