#Students to student_classes is a 'many to many' relationship
# many to many is 'one to many' in both directions
# Requires PKs from each of the other tables
# Usually requires three tables 1 -> junction -> 2
students:
id | first | last
=====================
1 | John | Lee
2 | Jane | Wilson
3 | Daniel | Gomez
classes:
id | name | teacher_id
==========================
1 | Biology | 2
2 | Physics | 4
3 | English | 77
enrollments
s_id | c_id
======================
1 | 2 # John is taking Physics
1 | 3 # John is taking English
2 | 2 # Jane is taking Physics
3 | 1 # Daniel is taking Biology
-- Getting all students for a class:
SELECT s.student_id, last_name
FROM enrollments
INNER JOIN students s ON s.student_id = enrollments.student_id
WHERE enrollments.class_id = X
-- Getting all classes for a student:
SELECT c.class_id, name
FROM enrollments
INNER JOIN classes c ON c.class_id = enrollments.class_id
WHERE enrollments.student_id = Y