SQL 102· Level 2

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

  • ON says which columns must match.
  • e and u are short nicknames for the tables. Write u.unit_name to 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 JOIN decides 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_idname
1Ana
2Ben
3Cai
enrolments
student_idunit_code
1STAT101
2ARTS102
4BUSN101

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
nameunit_code
AnaSTAT101
BenARTS102

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
nameunit_code
AnaSTAT101
BenARTS102
CaiNULL

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
nameunit_code
AnaSTAT101
BenARTS102
NULLBUSN101

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
nameunit_code
AnaSTAT101
BenARTS102
CaiNULL
NULLBUSN101

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.

Worked example

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
Getting the practice database ready…

This takes a few seconds the first time. Nothing leaves your computer.

Exercises in this level