skip to content
03, 11 chapters
joins and subqueries

joins and subqueries

putting tables back together. every join, semi and anti joins, subqueries, set operations and CTEs.

start part 03part 03 of 18, 11 chapters
  1. 03.01joins, in one picturea join is a filtered cross product. why the venn diagram lies to you.
  2. 03.02INNER JOINmatching rows across tables, ON vs USING vs NATURAL, and joining three tables in a row.
  3. 03.03LEFT, RIGHT and FULL joinskeep the rows that didn't match, padded with NULLs. and the WHERE that turns them back into inner joins.
  4. 03.04self joins and CROSS JOINa table joined to itself to find each employee's manager, and every pair of rows on purpose.
  5. 03.05join fan-outtwo one-to-many joins and your totals triple. aggregate first, then join.
  6. 03.06subqueriesa query wherever a value or a table can go. scalar, table and correlated subqueries.
  7. 03.07EXISTS, semi joins and anti joinsis there a match? the join that never duplicates, the one that keeps only the lonely rows, and relational division.
  8. 03.08the NOT IN trapone NULL in the list and NOT IN returns nothing at all. NOT EXISTS doesn't care.
  9. 03.09LATERAL joinsa subquery that can see the row to its left. the latest three orders per customer, in one join.
  10. 03.10UNION, INTERSECT and EXCEPTstacking results instead of joining them. and why UNION ALL is usually what you meant.
  11. 03.11WITH and CTEsnaming a subquery so you can read your own query. inlined by default, MATERIALIZED when you ask.