typestar

Joins & aggregation

22 steps in 5 sets of SQL.

Where SQL starts being worth learning properly. Aggregation first, then GROUP BY and HAVING, then the joins themselves: inner, left, cross, self, and the anti-join pattern that catches people out.

Set operations close it. Twenty-two steps, and the mental model to build is that a join produces rows and everything else filters or folds them.

Start this tour

Aggregation

Grouping

Joins

  • INNER JOINAn inner join keeps only the rows that match on both sides.
  • LEFT JOINA left join keeps every left row, filling the right side with NULL when nothing matches.
  • USING and column namesWhen both sides name the column the same thing, USING says so once.
  • RIGHT and FULL OUTER JOINLEFT keeps the left side's rows. RIGHT and FULL keep the others.
  • CROSS JOINEvery row on the left paired with every row on the right — on purpose, this time.
  • Self joinA table can join itself when a row points at another row of the same kind.
  • Joining three tablesJoins chain: each new table joins whatever has been assembled so far.
  • Finding rows with no matchThe anti join: an outer join, then keep only the rows that failed to match.
  • Aggregating across a joinGroup after joining to summarize the child rows per parent.

Set operations

Encore

  • sales_report.sqlA monthly sales report built from chained CTEs, one step per stage.
  • funnel_report.sqlCounting how many sessions made it to each step, then the drop between them.

The other SQL tours