How to use INNER JOIN

Join matching rows

An INNER JOIN returns combinations whose ON condition is true. The word INNER is optional.

SELECT s.name, e.course_id
FROM students AS s
INNER JOIN enrollments AS e
  ON e.student_id = s.id;

Join through a relationship

A many-to-many relationship commonly uses a linking table, so reaching the course name needs another join.

SELECT s.name, c.title
FROM students AS s
JOIN enrollments AS e ON e.student_id = s.id
JOIN courses AS c ON c.id = e.course_id;

Qualify shared names

Use aliases and qualified columns so readers and the database know which id or name you mean.

Check row counts

One-to-many joins repeat the “one” side for every matching child. Missing or incomplete join conditions can multiply rows unexpectedly.