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.
- 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
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 Type | Keeps Unmatched Left Rows? | Keeps Unmatched Right Rows? | Common Real-World Question |
|---|---|---|---|
INNER JOIN | No (dropped) | No (dropped) | "Show only completed orders alongside their buyer details." |
LEFT JOIN | Yes (fills right columns with NULL) | No | "Show ALL courses—even new courses with 0 students enrolled." |
RIGHT JOIN | No | Yes (fills left columns with NULL) | Rare; most engineers rewrite as a LEFT JOIN with tables swapped |
CROSS JOIN | Pairs every left row with every right row | N/A | Generating 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):
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:
- Start at
enrollments(e), grabbing a row such asstudent_id = 1, course_id = 10. - Match
e.student_idagainststudents.id(s.id) to attachs.full_name. - Match
e.course_idagainstcourses.id(c.id) to attachc.title.
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?"
- Start
FROM students sso every student stays in the result. LEFT JOIN enrollments e ON e.student_id = s.id: Students with courses get matched rows; students with zero courses getNULLinside everye.*column.- Add
WHERE e.id IS NULLto 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):
Click “Run Code” to execute this snippet right in your browser.
Quick Checkpoints
You want a report showing ALL courses—even brand-new courses that currently have 0 enrollments. Which JOIN should you write?
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?