I’m just wandering is the following query valid. I have one table called professor which contains professors. Table subject contains subjects. Many-to-many relation is realized with professor_subject table, which contains professor_id and subject_id fields.
Now I need to see which professor is teaching which subject. I wrote this SQL query:
SELECT concat(professor.name, " ", professor.surname) as "Professor",
subject.name as "Subject"
FROM professor_subject, subject, professor
WHERE subject.id = subject_id
and professor.id = professor_id;
Is this query valid ? I mean, will it always do what I want ? I’m little suspicious because I didn’t use JOIN keyword.
Thanks 🙂
No. There’s nothing here that correlates a professor to a subject. You’re using a cross join with no criteria (not sure if subject_id and professor_id in the where clause are table values or query parameters). You need something like: