skipnothing/SQL
UNIT 03

Joining Tables

INNER, LEFT, RIGHT, FULL, set operations, combining rows across tables

Real data lives in many tables. Joins pair rows across tables; outer joins keep the unmatched ones. Once you see joins as set operations on row-pairs, INNER vs LEFT vs FULL stops being syntax to memorize.

Loading…
BUILDS ON
TOPICS
#9

INNER JOIN

Pair rows from two tables on a join condition, predict the result row count when a row matches once or many times (fan-out), and pick between ON and USING.

Three users, four orders: the buyer's name sits in a separate table from the amount. Pair each order with its user by user_id and four matched rows come back; the user who never bought a thing is left out.

14 min
#10

LEFT, RIGHT & FULL JOIN

Keep the unmatched rows an inner join drops with LEFT, RIGHT, and FULL, read a NULL in the result as 'no match found' rather than missing data, and isolate rows with no partner by filtering the padded key with IS NULL.

Three users, but Cleo never ordered, so an inner join drops her. Keep her anyway: pin all three users into the result and let the order columns arrive empty wherever no user_id lines up.

14 min
#11

Self & Cross Joins

Alias a table into two copies so it can pair with itself for hierarchies and row comparisons, and drop the join condition with CROSS JOIN to generate every combination on purpose.

The employees table carries a manager_id column that points right back at its own id. Stand the table up beside a second copy of itself, and every worker can meet their boss on a single row.

12 min
#12

UNION, INTERSECT, EXCEPT

Stack two query results with UNION, INTERSECT, and EXCEPT, drop the duplicate-removal sort with UNION ALL when rows cannot overlap, and line the columns up by position before a reordered select list files the wrong data under the right name.

Two buyers placed three orders in January, two ordered in February, and one shows up in both months. Asking for everyone, only the repeat buyers, or only the January-only crowd is the same two SELECT results stacked three different ways.

11 min
UNLOCKS
All units