The basic syntax of an INNER JOIN
is:
SELECT columns
FROM table1
INNER JOIN table2 ON table1.column = table2.column;
table1
, table2
: The two tables to join.column
: The column(s) used to match rows in both tables.Let’s assume we have two tables: employees
and departments
. We want to fetch the names of employees along with the departments they belong to. Here’s how you would use INNER JOIN
:
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
In this example:
employees
table with the departments
table using the INNER JOIN
.department_id
from the employees
table and the id
from the departments
table.name
and the corresponding department_name
.The INNER JOIN
is a powerful tool for combining data from two or more tables in MySQL. It helps retrieve only the rows with matching values, ensuring that the result contains relevant data from both tables. Using INNER JOIN
efficiently is essential for database querying and allows you to work with related data across multiple tables.