All concepts

SQL Joins

Match rows across tables on a key; the join type decides which non-matches survive.

SQL · Beginner · ~4 min

In plain English

Stitch two tables together on a shared key. Inner keeps only matches; left keeps everything on the left and fills the gaps with NULL.

Why it's worth your time

It's the highest-frequency interview topic in SQL, and the most common source of quietly wrong numbers.

If you remember three things

  • INNER: matches only. LEFT: all of the left, NULLs on the right
  • A duplicate key on either side multiplies rows
  • Filtering a LEFT JOIN's right table in WHERE turns it into an INNER JOIN

Overview

A join stitches rows from two tables into one wider row, matched on a shared key — usually a primary key on one side and a foreign key on the other. INNER JOIN keeps only matched rows; LEFT, RIGHT, and FULL OUTER joins additionally keep unmatched rows from one or both sides, filling missing columns with NULL.

In an interview

Joins combine tables on a join condition. INNER keeps rows that match on both sides. LEFT keeps every left row and NULL-fills where the right has no match; RIGHT and FULL extend that to the other side. Index the join columns so matching is a lookup, not a scan.

Production defaults

Check cardinality
before joining. One-to-many is fine; many-to-many silently inflates every count
LEFT JOIN filters
put conditions on the right table in ON, not WHERE, or you lose the unmatched rows
Verify
compare row counts before and after. An unexpected increase means a fan-out

What breaks

  • Row count exploded after a join — Duplicate keys on one side. Deduplicate or aggregate before joining.
  • LEFT JOIN behaving like INNER — A WHERE condition on the right table's column. Move it into the ON clause.

Watch it explained

Inner Join, Left Join, Right Join and Full Outer Join in SQL Server | SQL Server Joins — Questpond, 8:11

Related