SteelReed

Search

Search posts, questions…

SQL & Query TuningQuestion 7 / 37

Junior

What JOIN types are there, and how does INNER JOIN differ from LEFT JOIN?

Short answer

INNER JOIN keeps only row pairs that match in both tables; LEFT JOIN keeps every left row, with unmatched right columns as NULL. JOIN combines horizontally (adds columns); appending rows is UNION's job.

only matchesevery left rowunmatched left rowsrelation in one tableWhich rows to keep?INNER JOINLEFT JOINAnti-join:NOT EXISTSSelf join

3 points to say:

  1. INNER keeps only rows matching in both tables; LEFT keeps every left row, unmatched right side becomes NULL.
  2. Anti-join: LEFT JOIN ... WHERE right.id IS NULL or NOT EXISTS.
  3. Self join for relations within one table (employee and manager); JOIN adds columns, UNION adds rows.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.