SQL & Databases·Relating and summarising

Joins

Combine tables through keys, and know when each join type is appropriate.

Beginner 16 min read Lesson 7 of 480 Challenge included
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.

joins.sqlSQL
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;
JoinKeeps
INNER JOINOnly rows that match on both sides.
LEFT JOINAll left rows, plus matches on the right.
RIGHT JOINAll right rows, plus matches on the left.
FULL JOINAll rows from both sides where supported.
Your turn

Course with lesson counts

Hard

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 c
Check 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 in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 7 of 480 · 0% of course