intermediate30 min
JOINs & Relationships
Combine data across multiple tables with INNER JOIN, LEFT JOIN, and more
Why JOINs Matter
Real databases rarely use a single table. Data is split across related tables to avoid duplication. JOINs let you combine data from multiple tables in a single query.
Setting Up Related Tables
Output
Run your code to see output here
INNER JOIN
Returns only rows that have a match in both tables:
Output
Run your code to see output here
LEFT JOIN
Returns all rows from the left table, even if there's no match in the right table (NULL fills the gaps):
Output
Run your code to see output here
JOINs Summary
| Type | Returns |
|---|---|
INNER JOIN | Only rows with matches in both tables |
LEFT JOIN | All rows from left table + matches from right |
RIGHT JOIN | All rows from right table + matches from left |
FULL JOIN | All rows from both tables (matched where possible) |
CROSS JOIN | Every combination of rows (Cartesian product) |
GROUP BY with Aggregates
Output
Run your code to see output here
Check Your Understanding
Which JOIN returns all students even if they're not enrolled in any course?
Exercise
Write a query that lists all courses with the count of enrolled students. Include courses with zero enrollments.
Python will load on first run