← Back to Chapters

MySQL INNER JOIN

? MySQL INNER JOIN

? Quick Overview

The INNER JOIN keyword selects records that have matching values in both tables. It returns only rows where a match exists between the joined tables' columns.

? Key Concepts

  • Match-only results: INNER JOIN excludes rows without matching counterparts.
  • Join condition: Use ON to specify the equality between common columns.
  • Multiple joins: You can chain multiple INNER JOIN clauses to combine several tables.

? Syntax / Theory

? View Code Example
-- Syntax for INNER JOIN
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;

? Code Example(s)

? View Code Example
-- Get all students and their corresponding courses
SELECT students.name, courses.course_name
FROM students
INNER JOIN courses
ON students.course_id = courses.course_id;
? View Code Example
-- Get all orders and their related customer information
SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers
ON orders.customer_id = customers.customer_id;

? Live Output / Explanation

How results behave

The queries above will return only rows where the join condition matches. For example, the student-course query returns rows for students who have a course_id that exists in the courses table. Any student without a matching course_id will be excluded.

Use LEFT JOIN if you want to include all rows from the left table even when there is no match on the right table.

✅ Tips & Best Practices

  • Prefer joining on indexed columns for better performance.
  • Always qualify columns with table names when joining multiple tables to avoid ambiguity.
  • Verify join conditions when chaining multiple joins to prevent accidental row multiplication.

? Try It Yourself / Practice Tasks

  • Write a query to find all students who have enrolled in any course using INNER JOIN.
  • List all customers and their orders using INNER JOIN between customers and orders.
  • Join three tables (e.g., orders → customers → products) and ensure correct join conditions to retrieve product names per order.