MySQL·MySQL from first query to production

Joins

Combine tables with INNER, LEFT and self joins without multiplying rows by accident.

Beginner 14 min read Lesson 3 of 300 Challenge included
What you will be able to do
  • Join on indexed keys.
  • Use LEFT JOIN when the right side is optional.
  • Alias tables for readable SQL.
  • Watch for duplicate rows when one side has many matches.

In plain English

A join matches rows from two tables on a condition. INNER keeps matches, LEFT keeps every left row, and a self join uses two aliases of the same table. Check cardinality before trusting a join.

joins.sqlSQL
SELECT c.title AS course, l.title AS lesson, l.minutes
FROM courses c
JOIN lessons l ON l.course_slug = c.slug
WHERE c.slug = 'css'
ORDER BY l.minutes DESC;
Your turn

List courses with lesson counts

Medium

Join courses to lessons and count lessons per course.

SQL · starter
SELECT c.title FROM courses c
Check your understanding before moving on.
Knowledge checkQuestion 1

Which join keeps every row from the left table?

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.

Next: Aggregation
Lesson 3 of 300 · 0% of course