Joins
Combine tables through keys, and know when each join type is appropriate.
What you will be able to do
- Identify primary and foreign keys
- Write inner and left joins
- Avoid fan-out mistakes
Tables stay small and focused. Joins reconnect them through shared keys: a lesson belongs to a course, so lessons.course_slug references courses.slug.
SELECT c.title AS course, COUNT(l.id) AS lessons
FROM courses c
LEFT JOIN lessons l ON l.course_slug = c.slug
GROUP BY c.title
ORDER BY lessons DESC;| Join | Keeps |
|---|---|
| INNER JOIN | Only rows that match on both sides. |
| LEFT JOIN | All left rows, plus matches on the right. |
| RIGHT JOIN | All right rows, plus matches on the left. |
| FULL JOIN | All rows from both sides where supported. |
Your turn
HardCourse with lesson counts
List every course title with its number of lessons, including courses with zero.
SQL · starter
SELECT c.title, COUNT(l.id) AS lessons
FROM courses cCheck your understanding before moving on.
Knowledge checkQuestion 1
Which join keeps every row from the left table even without a match?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.