JOINs
โ Report an issue with this lessonReal data lives across multiple related tables. A JOIN combines rows from two tables based on a matching column.
SELECT users.name, enrollments.purchased_at
FROM users
JOIN enrollments ON users.id = enrollments.user_id;
An INNER JOIN (what plain JOIN means) only
returns rows that match in both tables. A LEFT JOIN keeps
every row from the left table even if there's no match on the right โ
useful for "show me every user, including ones with zero
enrollments":
SELECT users.name, COUNT(enrollments.id) AS course_count
FROM users
LEFT JOIN enrollments ON users.id = enrollments.user_id
GROUP BY users.name;
Try it yourself
Exercise: Using the users and enrollments tables below, write a JOIN selecting each user's name alongside the purchased_at date of their enrollments.
Expected output: open-ended โ there's no single correct output here, just get your code running without errors.
Run your code and get it working before marking this lesson complete.
// that was the last free lesson
9 more lessons โ including Project: design a small course-platform schema โ plus a certificate are waiting.
Unlock the full course โ $59.99