Lesson Description
Combine rows from two tables where a condition matches.
Lesson 1 of 4
Progress: 0/4 exercises solved (0%)
Solve all exercises below to unlock the next lesson.
Lesson Description
Combine rows from two tables where a condition matches.
Easy Project
Mini project: Build a people-and-departments report that lists employee names, department names, and validates row counts to prove your join is correct.
JOIN Visual
employees
departments
Match keys first, then verify row counts after the join.
A JOIN (also written INNER JOIN) combines rows from two tables where a condition is met. Rows without a match in either table are excluded.
SELECT t1.col, t2.col
FROM table1 t1
JOIN table2 t2 ON t2.foreign_key = t1.primary_key;
SELECT
e.first_name,
e.last_name,
d.name AS department
FROM employees e
JOIN departments d ON d.id = e.department_id;
e and d are table aliases — shorter names assigned for the query.
e.id vs d.id).You can chain JOINs. Each adds one more table:
SELECT e.first_name, d.name AS dept, j.title AS job
FROM employees e
JOIN departments d ON d.id = e.department_id
JOIN jobs j ON j.id = e.job_id;
If table A has 100 rows and table B has 50 rows, the JOIN returns at most 100 rows (one per employee). It can return more if the ON condition matches multiple rows in B (a one-to-many match multiplies rows).
JOIN and INNER JOIN are identical — both exclude non-matching rows.ON — a JOIN without ON is a CROSS JOIN (cartesian product).This lesson is part of Joins. Focus on the core idea in INNER JOIN, then validate with deliberate practice.
What to master
Common mistakes
High-level strategy
Task ladder
Transparent data checks
Retention loop
Logical reasoning for commands
JOIN ... ON
Why: Connects rows only when keys truly match.
Memory cue: No key match = no trusted relationship.
LEFT JOIN
Why: Keeps base table rows even when lookup data is missing.
Memory cue: Left keeps the story intact.
COUNT(DISTINCT key)
Why: Confirms joins did not multiply business entities.
Memory cue: Distinct key count should stay believable.
Concept check
3 quick questions. One at a time. Instant score at the end.
Return first_name, last_name, and the department name (alias department) for every employee that has a department.
Return order_id, product name (alias product), quantity, and unit_price for every order item.
From the hospital sandbox, return patient first_name (alias patient_first), patient last_name (alias patient_last), doctor first_name (alias doctor_first), and doctor last_name (alias doctor_last) for every appointment.
From the store sandbox, return product name (alias product), category name (alias category), and price, ordered by category, then product.
Lesson 1 of 4
0/4 solved (0%)
Finish this lesson to unlock next.