I have this query that I am trying to write out:
Which students (names) have taken more than one course from the same professor?
I have a students, course, and professor table. This is the ER diagram:
I have 4 tables:
Students
(student_id
is the PK)Courses
(course_id
is the PK andprofessor_id
is the FK to the Professor Table)Takes_courses
(student_id
andcourse_id
is the PK)Professor
(professor_id
is the PK)
My plan:
Select student_name
From Student
Where student_id > Some (Select *
From Courses Natural Join Takes_Courses
Group By professor_id)
I am not able to run this query above. How am I supposed to count the number of student_id appears in the inner query?
Best Answer
Testbed:
The query below:
Finds all unique student/professor/course combinations (so we don't get results for students who take the same course twice — not sure that is possible from the question).
Groups the student/professor pairings and filters out those that only match a single course.
Uses the resulting student IDs to look up the student names.
dbfiddle here