Combine tables with INNER JOIN, LEFT JOIN, and RIGHT JOIN
Use table aliases for readable multi-table queries
Understand when each join type is appropriate
📊 JOIN Visualizer
See the students and enrollments tables side by side, then click a JOIN type to see what rows are combined and returned.
students table
enrollments table
Why JOINs?
Relational databases split data across tables to avoid repetition. A student's name is stored once in the students table — not repeated in every enrollment. JOINs combine related rows from multiple tables based on matching key values.
Primary key (PK) — a unique identifier for each row (e.g., students.id).
Foreign key (FK) — a column that references a PK in another table (e.g., enrollments.student_id references students.id).
JOIN Types
INNER JOIN — returns only rows that have a match in BOTH tables. Most common.
LEFT JOIN — returns all rows from the left (first) table plus matching rows from the right. Rows with no match get NULL for right table columns.
RIGHT JOIN — all rows from the right table, matching rows from the left. Less common; usually rewrite as a LEFT JOIN.
FULL OUTER JOIN — all rows from both tables. Unmatched rows get NULL on the other side.
Quick Check
1. What is a foreign key?
A key from another country's database
A column that references the primary key of another table, linking related rows
A primary key with duplicate values
An encrypted primary key
2. What does an INNER JOIN return?
All rows from both tables
Only rows that have a matching value in both tables
All rows from the left table, with NULLs for non-matching right rows
The intersection of primary keys
3. You want to find all students, including those who haven't enrolled in any course yet. Which JOIN should you use?
INNER JOIN
LEFT JOIN (students on the left)
RIGHT JOIN
No JOIN needed
4. In SELECT s.name, e.course FROM students s JOIN enrollments e ON s.id = e.student_id, what does s represent?
The score column
A table alias for "students" — a shorthand to avoid writing the full table name