← Back to Chapters

MySQL INNER JOIN

? MySQL INNER JOIN

? Quick Overview

The INNER JOIN is a commonly used operation in SQL to combine records from two or more tables based on a related column. It acts as a filter, returning only the rows where there is a match in both tables.

A B

? Key Concepts

  • Joins multiple tables using a Common Column (usually Primary/Foreign Keys).
  • Returns only the Intersection of both datasets.
  • Data missing in either table for a specific ID will result in that row being excluded.

? Syntax / Theory

? View SQL Syntax
-- INNER JOIN syntax structure
SELECT table1.col, table2.col
FROM table1
INNER JOIN table2
ON table1.id = table2.fk_id;

? Code Example

Retrieve employee names with their corresponding department names.

? View Code Example
-- Joining employees and departments tables
SELECT employees.name, departments.dept_name
FROM employees
INNER JOIN departments
ON employees.dept_id = departments.id;

? Interactive Example

Try Joining These Tables

Table: Users

1. Alice (ID: 101)
2. Bob (ID: 102)
3. Charlie (ID: 103)

Table: Orders

• Pizza (User: 101)
• Burger (User: 102)
• Taco (User: 105)
Click the button to see matching records...

? Use Cases

  • Displaying orders for a specific user in an E-commerce site.
  • Linking blog posts to their respective categories.
  • Generating payroll reports by joining "Employee" and "Salary" tables.

✅ Tips & Best Practices

  • Table Aliases: Use FROM users AS u to keep queries short.
  • Indexing: Always index columns used in the ON clause for speed.
  • Ambiguity: Always prefix column names with table names (e.g., users.id) to avoid errors.

? Try It Yourself

  • Change the join to LEFT JOIN to see how Charlie appears.
  • Add a WHERE clause to filter the joined results.
  • Perform a join between three tables (Users, Orders, and Products).