JOINs
Information is spread across tables. An enrolment holds a unit_code but not the unit's name, which lives in units. A JOIN puts matching rows from two tables side by side.
FROM enrolments e JOIN units u ON e.unit_code = u.unit_code
ONsays which columns must match.eanduare short nicknames for the tables. Writeu.unit_nameto say which table a column comes from. You must do this when both tables have a column with the same name.- The word in front of
JOINdecides what happens to rows that have no match in the other table. There are four choices, shown below.
When a joined query gives a surprising count, check the join type first.
The four types of join
The two tables in these examples
These are two tiny made-up tables, small enough to follow every row. Notice two things: Cai has no enrolment, and the BUSN101 enrolment belongs to student 4, who is not in the students table.
students| student_id | name |
|---|---|
| 1 | Ana |
| 2 | Ben |
| 3 | Cai |
enrolments| student_id | unit_code |
|---|---|
| 1 | STAT101 |
| 2 | ARTS102 |
| 4 | BUSN101 |
Every example below runs the same query and changes only the join word:
SELECT s.name, e.unit_code FROM students s JOIN enrolments e ON s.student_id = e.student_id
The table written first (students) is called the left table. The one after the join word (enrolments) is the right table.
JOIN (also written INNER JOIN)
Keeps only the rows that have a match in both tables. Cai drops out, having no enrolment. The BUSN101 enrolment drops out because its student is missing.
students JOIN enrolments gives 2 rows| name | unit_code |
|---|---|
| Ana | STAT101 |
| Ben | ARTS102 |
Use it when you only want complete pairs. Be aware that it quietly leaves rows out.
LEFT JOIN
Keeps every row from the left table, matched or not. Where there is no match, the right table's columns are NULL. Cai is back, with no unit.
students LEFT JOIN enrolments gives 3 rows| name | unit_code |
|---|---|
| Ana | STAT101 |
| Ben | ARTS102 |
| Cai | NULL |
Use it for questions like "all students, including those with no enrolments". It is the join you will see most after the plain JOIN.
RIGHT JOIN
The mirror image: keeps every row from the right table. Where there is no match, the left table's columns are NULL. The BUSN101 enrolment is back, with no name.
students RIGHT JOIN enrolments gives 3 rows| name | unit_code |
|---|---|
| Ana | STAT101 |
| Ben | ARTS102 |
| NULL | BUSN101 |
It is rare in practice. Most people swap the two tables and write a LEFT JOIN instead, which gives the same rows.
FULL JOIN (also written FULL OUTER JOIN)
Keeps every row from both tables. Matched rows are paired, and unmatched rows from either side appear with NULLs in the other side's columns.
students FULL JOIN enrolments gives 4 rows| name | unit_code |
|---|---|
| Ana | STAT101 |
| Ben | ARTS102 |
| Cai | NULL |
| NULL | BUSN101 |
Use it to reconcile two lists: the NULLs show what is missing from each side.
Which one to use
- Only rows that match on both sides:
JOIN - Everything from the first table:
LEFT JOIN - Everything from the second table:
RIGHT JOIN - Everything from both:
FULL JOIN
The same question gave 2, 3, 3 and 4 rows here. That is why the join type is the first thing to check when a count looks wrong.
Which faculty teaches each unit?
SELECT u.unit_code, u.unit_name, f.faculty_name FROM units u JOIN faculties f ON u.faculty_id = f.faculty_id ORDER BY u.unit_code
This takes a few seconds the first time. Nothing leaves your computer.