2. Designing Tables & Combining Data

Combining Tables with INNER JOIN & LEFT JOIN

Stitch normalized tables back together with SQL JOINs: master INNER JOIN vs LEFT JOIN visually, understand foreign-key matching, use clean aliases, and query live.

4 min readUpdated 2026-11-29
In This Lesson
  • Connect normalized tables cleanly using PRIMARY and FOREIGN keys
  • Know exactly when to choose INNER JOIN vs LEFT JOIN
  • Join 3 tables together and spot unmatched rows cleanly

Earlier we learned why good databases split facts into separate normalized tables—students here, courses there, and enrollments storing only numeric ID pairs (student_id = 1, course_id = 20). That stops duplicate data bugs, yet reading raw numbers like student_id = 1 does not help humans reading a dashboard.

JOIN is how SQL pulls separated tables back together on the fly into one clean report.

Visual Mental Model: How INNER JOIN & LEFT JOIN Differ

INNER JOIN vs LEFT JOIN

INNER JOIN

Keeps only rows where both courses.id and enrollments.course_id match.

LEFT JOIN

Keeps every row from the left table (`courses`), returning NULL if no enrollment exists.

INNER JOIN drops unmatched courses; LEFT JOIN keeps every course even when enrollment count is 0.

Think carefully about what happens when a row on one side has no match on the other side:

Join TypeKeeps Unmatched Left Rows?Keeps Unmatched Right Rows?Common Real-World Question
INNER JOINNo (dropped)No (dropped)"Show only completed orders alongside their buyer details."
LEFT JOINYes (fills right columns with NULL)No"Show ALL courses—even new courses with 0 students enrolled."
RIGHT JOINNoYes (fills left columns with NULL)Rare; most engineers rewrite as a LEFT JOIN with tables swapped
CROSS JOINPairs every left row with every right rowN/AGenerating size × color product grids (S/M/L × Red/Blue)

Joining 3 Tables with Clean Aliases

Look at enrollments: every row stores student_id and course_id. To print the student's actual full_name and the course's title, we chain two INNER JOIN clauses together using short table aliases (e, s, c):

sql
SELECT
  s.full_name,
  c.title AS course_title,
  e.enrolled_on
FROM enrollments e
INNER JOIN students s ON e.student_id = s.id
INNER JOIN courses c ON e.course_id = c.id
ORDER BY e.enrolled_on ASC;

Walk through how SQL builds each output row:

  1. Start at enrollments (e), grabbing a row such as student_id = 1, course_id = 10.
  2. Match e.student_id against students.id (s.id) to attach s.full_name.
  3. Match e.course_id against courses.id (c.id) to attach c.title.
Forgetting Table Prefixes When Both Tables Have an `id` Column

Both students and enrollments have a column named id. Writing bare SELECT id after a JOIN causes an ambiguous column name: id error! Always qualify shared names with their table alias (s.id or e.id).

Finding "Missing" Records Using LEFT JOIN + IS NULL

One of SQL's most practical interview and reporting tricks answers negative questions—like "Which students signed up for an account but have NOT enrolled in a single course yet?"

  1. Start FROM students s so every student stays in the result.
  2. LEFT JOIN enrollments e ON e.student_id = s.id: Students with courses get matched rows; students with zero courses get NULL inside every e.* column.
  3. Add WHERE e.id IS NULL to keep only those unmatched students!

Try It Yourself: Live SQL Playground

Click Run Code to view all joined enrollments, then modify the query to find students with zero enrollments (LEFT JOIN enrollments e ... WHERE e.id IS NULL):

SQL (SQLite)Live SQL: INNER JOIN & LEFT JOIN
Output

Click “Run Code” to execute this snippet right in your browser.

Quick Checkpoints

Check Your Understanding

You want a report showing ALL courses—even brand-new courses that currently have 0 enrollments. Which JOIN should you write?

Check Your Understanding

Why does SQL throw an ambiguous column error if you write SELECT id FROM students s JOIN enrollments e ON e.student_id = s.id?