INNER JOIN & keys

Connect related tables

Lesson 10 of 20 · 11 min · Module 4: Relationships & real reports

A relational database separates related concepts into tables. Joins reconnect those records through matching key values.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Join tables through matching keys
  • Qualify ambiguous column names
  • Recognize that a join can produce multiple rows per learner

The idea

The enrollments table connects a learner to a course using learner_id and course_id. Join learners to enrollments, then enrollments to courses. Short aliases keep qualified column names readable.

SELECT l.name, c.title
FROM learners AS l
JOIN enrollments AS e ON e.learner_id = l.id
JOIN courses AS c ON c.id = e.course_id;

01Read the relationship

A learner can have several enrollments, and a course can have several learners. Each enrollment stores one connection. Joining on the correct keys preserves that relationship.

02Distinguish profile data from enrollment data

learners.language is a profile field, not a complete enrollment list. Alex and Sam are enrolled in SQL even though their profile languages differ. The enrollment table answers this question.

Your turn

Return learner name and course title for enrollments in the course whose language is 'SQL'. Sort learners alphabetically. Use the courses and enrollments tables, not learners.language.

What the checks look for

  • Finds Alex, Lee, and Sam in SQL Essentials
Try it in the editor →

Take this with you

Choose the table relationship that actually represents the question.

Learn SQL Foundations for free

20 short lessons with real code and instant checks.

See the whole course →