Lesson 2.1

Joins and set operations

An inner join answers "rows that match"; a left join answers "all of these, with matches where they exist".

11mIntermediate13.9k students

Overview

The join type is part of the question

An inner join answers "rows that match"; a left join answers "all of these, with matches where they exist". Reaching for an inner join when you meant the second quietly drops exactly the rows you were looking for.

Joining on a non-unique column multiplies rows, and an aggregate over the result then double-counts. If a total is suspiciously high, count the rows before and after the join.

Set operations are sometimes clearer than a join. Asking which ids exist here but not there reads better as an except than as a left join with a null check.

In this lesson you will:

  • Choose the join that matches the question
  • Avoid accidentally multiplying rows
  • Use set operations where they fit

Resources

Previous Lesson
Next Lesson
Joins and set operations — PostgreSQL for Application Developers — Vertex