Through a join table
After this: Join students to courses through enrolments, three tables in one query.
On this page · 6 sections · 3 checks
A course has one teacher, so one column, teacher_id, was enough to link them. Students and courses are different. Ana takes three courses, and Maths has four students. Which column could hold that? This lesson joins students to courses, and needs a third table to do it.
Many on both sides
Try to give each student a course_id column. Ana needs three values in it, and one column of one row holds one value. Give each course a student_id column instead, and Maths needs four. Neither side can hold the link.
When many rows on each side can belong together, the link is called . The answer is a table whose only job is to hold the pairs: enrolments. Each enrolment row says "this student takes this course", with the mark for it. Such a table is a .
By hand first
Ana's id is 1. Her rows in enrolments are:
| student_id | course_id | mark |
|---|---|---|
| 1 | 1 | 72 |
| 1 | 2 | 65 |
| 1 | 3 | 80 |
To turn them into names, do two lookups for each row:
student_id1 is Ana, in students.course_id1 is Maths, 2 is Physics, 3 is History, in courses.- So Ana takes Maths (72), Physics (65) and History (80).
Each enrolment row needs one student and one course. That is two joins.
Three tables in one query
Read it in the order the database works:
FROM enrolments AS estarts from the 10 enrolment rows. Starting from the join table is natural: each of its rows is one pair.JOIN students AS s ON s.id = e.student_idputs the student row beside each enrolment. Still 10 rows, now wider.JOIN courses AS c ON c.id = e.course_idputs the course row beside each one. Still 10 rows, wider again.- SELECT keeps the student's name, the course's title and the mark, and ORDER BY sorts by name, then title.
Each enrolment finds exactly one student and one course, so the result has one row per enrolment: 10. Chloe's History row shows NULL as its mark, because her History work has not been marked yet. Eve is not in the result at all: she has no enrolment, so no row starts from her. Lesson 4 brings her back.
Step through it
Now add a WHERE to keep only Maths, and sort by mark, best first. Step through it and watch the row count at each step.
SELECT s.name, c.title, e.markFROM enrolments AS eJOIN students AS s ON s.id = e.student_idJOIN courses AS c ON c.id = e.course_idWHERE c.title = 'Maths'ORDER BY e.mark DESC;enrolments · 10 rows
| student_id | course_id | mark |
|---|---|---|
| 1 | 1 | 72 |
| 1 | 2 | 65 |
| 1 | 3 | 80 |
| 2 | 1 | 58 |
| 2 | 4 | 90 |
| 3 | 1 | 85 |
| 3 | 3 | NULL |
| 4 | 2 | 47 |
| 4 | 4 | 76 |
| 6 | 1 | 91 |
students · 6 rows
| id | name | year | city |
|---|---|---|---|
| 1 | Ana | 2 | Leeds |
| 2 | Ben | 1 | York |
| 3 | Chloe | 2 | Leeds |
| 4 | Dev | 3 | Hull |
| 5 | Eve | 1 | York |
| 6 | Femi | 3 | Leeds |
courses · 5 rows
| id | title | credits | teacher_id |
|---|---|---|---|
| 1 | Maths | 4 | 1 |
| 2 | Physics | 4 | 1 |
| 3 | History | 3 | 2 |
| 4 | Art | 2 | 2 |
| 5 | Music | 2 | 3 |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
The FROM step has 10 rows. The first JOIN step adds the students columns and keeps 10. The second JOIN step adds the courses columns and keeps 10. WHERE then keeps the 4 rows whose title is Maths. The answer: Femi 91, Chloe 85, Ana 72, Ben 58.
Notice that WHERE tests c.title, a column that came from the courses table. After the joins, every row carries the columns of all three tables, and WHERE, GROUP BY and ORDER BY can use any of them.
The pattern
To go from one side of a many-to-many link to the other:
- Start from the join table, or from one side and join the join table to it.
- Join each table the join table points at, with an ON test per foreign key.
- Filter, group and sort as usual.
The number of joins is the number of tables minus one: three tables, two joins.
Check yourself
Why does the school not give the students table a course_id column, the way courses has a teacher_id column?
This query joins all three tables and has no WHERE. How many rows does it give?
SELECT s.name, c.title, e.markFROM enrolments AS eJOIN students AS s ON s.id = e.student_idJOIN courses AS c ON c.id = e.course_idORDER BY s.name, c.title;Which courses does this query list?
SELECT c.titleFROM enrolments AS eJOIN students AS s ON s.id = e.student_idJOIN courses AS c ON c.id = e.course_idWHERE s.name = 'Ana'ORDER BY c.title;- Say why students and courses need a join table: each side has many of the other, and one column holds one value.
- Join three tables, enrolments, students and courses, with one ON test per foreign key.
- Say how many rows the join gives: one per enrolment, 10, because each finds one student and one course.
- Test, group and sort on a column of any joined table, such as c.title.