Joining two tables
After this: Write a JOIN with an ON test, use short names for tables, and count per teacher across a join.
On this page · 6 sections · 3 checks
In the last lesson you found History's teacher in two steps: read the teacher_id in the course row, then find the teacher row with that id. That is fine for one course. This lesson hands the same lookup to the database, for every course at once.
The idea in words
Take the rows of courses one at a time. For each one, look through the teachers and find the row whose id equals the course's teacher_id. Put the two rows side by side, as one wider row. When every course has been through this, you have a table of courses with their teachers next to them.
By hand first
- Maths has
teacher_id1. Teacher 1 is Mrs Okafor. Row: Maths, Mrs Okafor. - Physics has 1 as well. Row: Physics, Mrs Okafor.
- History has 2: Mr Silva. Art has 2: Mr Silva.
- Music has 3: Ms Chen.
Five courses, five rows. Mrs Okafor and Mr Silva each appear twice, because each teaches two courses.
The query
SELECT courses.title, teachers.nameFROM coursesJOIN teachers ON teachers.id = courses.teacher_idORDER BY courses.id;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 |
teachers · 3 rows
| id | name | room |
|---|---|---|
| 1 | Mrs Okafor | R1 |
| 2 | Mr Silva | R2 |
| 3 | Ms Chen | R3 |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
Step through it. The FROM step starts with the 5 course rows. At the JOIN step the teachers columns appear beside them: each course row now carries the teacher row that matched it. SELECT then keeps two columns, and ORDER BY puts the rows in course order.
Read it line by line:
FROM coursesstarts from the courses table, as in every query so far.JOIN teachersbrings in a second table. Its rows are paired with the course rows.ON teachers.id = courses.teacher_idis the test for a pair. A course row and a teacher row are put together only when the teacher'sidequals the course'steacher_id.SELECT courses.title, teachers.namepicks one column from each table. The table name in front says which table each column comes from.ORDER BY courses.idsorts by the course's key.
The ON test is the heart of a join. It says which rows belong together, almost always by comparing a foreign key with the key it points at.
A plain JOIN is called an : it keeps only pairs that pass the ON test. A row that finds no partner is left out. Here every course has a teacher, so all five courses stay. Lesson 4 shows what happens when a row has no partner.
Short names for tables
Writing teachers. and courses. in front of every column gets long. You can give each table a short name after AS, and use it everywhere in the query:
teachers AS t means "call this table t in this query". Such a short name is an . You already gave columns new names with AS in "Reading data"; this is the same word for tables. The query also starts from teachers this time, and the ON test is written the other way round. The pairs are the same.
One teacher, many courses
Each course has one teacher, and a teacher can have many courses. This shape is called . The foreign key sits on the "many" side: teacher_id is a column of courses, not of teachers.
Because the join gives one row per course, you can group those rows by teacher and count them, as in "Counting and grouping":
The join runs first and makes the five rows. GROUP BY then puts them in three groups, one per teacher. COUNT(*) counts the course rows in each group: 2, 2 and 1. SUM(c.credits) adds their credits: Maths and Physics give Mrs Okafor 4 + 4 = 8, History and Art give Mr Silva 3 + 2 = 5, and Music gives Ms Chen 2.
The query groups by t.id and not only by the name, because two teachers could share a name. It lists t.name too, following the rule from "Counting and grouping": every selected column is in GROUP BY or inside a function like COUNT.
Check yourself
The same join, with a WHERE line added. Which rows does it give?
SELECT courses.titleFROM coursesJOIN teachers ON teachers.id = courses.teacher_idWHERE teachers.name = 'Mr Silva'ORDER BY courses.id;Write the missing line, so that each course is matched with the teacher who teaches it and the query gives the five rows from the lesson.
SELECT courses.title, teachers.nameFROM coursesJOIN teachers______________ -- ← the missing lineORDER BY courses.id;Someone wrote courses.id where courses.teacher_id belongs. How many rows does this give, and are they right?
SELECT courses.title, teachers.nameFROM coursesJOIN teachers ON teachers.id = courses.idORDER BY courses.id;- Write FROM, JOIN and an ON test that compares a foreign key with the key it points at.
- Say that a plain JOIN (an inner join) keeps only the pairs that pass the ON test.
- Give tables short names with AS, and write each column as table.column when two tables share a column name.
- Count and add up per teacher across a join: the join runs first, then GROUP BY makes the groups.