In SQL, an INNER JOIN is one of the most commonly used types of join. It allows you to combine rows from two or more tables based on a related column. When you use an INNER JOIN, only the rows that have matching values in both tables are included in the result set.
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
An INNER JOIN is used to select records from two or more tables that have matching values in the specified columns. It returns the intersection of both tables, meaning that only rows with a match in both tables will appear in the result set.
The INNER JOIN operates on the concept of relational databases, where tables are linked by primary and foreign key relationships.
The syntax for an INNER JOIN is straightforward:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Consider two tables:
-- employees table
+-------------+-----------------+---------------+
| employee_id | employee_name | department_id |
+-------------+-----------------+---------------+
| 1 | Alice | 1 |
| 2 | Bob | 2 |
| 3 | Charlie | 1 |
| 4 | David | NULL |
+-------------+-----------------+---------------+
-- departments table
+---------------+--------------------+
| department_id | department_name |
+---------------+--------------------+
| 1 | HR |
| 2 | Finance |
+---------------+--------------------+
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
Explanation:
employee_id
, employee_name
, and department_name
from the employees
and departments
tables.INNER JOIN
to match rows where employees.department_id
equals departments.department_id
.Result:
employee_id | employee_name | department_name |
---|---|---|
1 | Alice | HR |
2 | Bob | Finance |
3 | Charlie | HR |
Explanation of Result:
department_id
) appear in the result.NULL
department, is excluded because there's no matching department in the departments
table.You can combine multiple INNER JOINs in a single query to retrieve data from more than two tables. When joining multiple tables, you need to specify the join condition for each one.
Let's say you also have a projects
table that stores information about the projects each employee works on:
-- projects table
+------------+-------------+----------------+
| project_id | project_name| employee_id |
+------------+-------------+----------------+
| 1 | Project X | 1 |
| 2 | Project Y | 2 |
| 3 | Project Z | 3 |
+------------+-------------+----------------+
Now, to get the employee_name
, department_name
, and project_name
for each employee, you can use multiple INNER JOINs:
SELECT employees.employee_name, departments.department_name, projects.project_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id
INNER JOIN projects ON employees.employee_id = projects.employee_id;
Explanation:
employees
, departments
, and projects
.employees
and departments
based on department_id
, and then joins employees
and projects
based on employee_id
.Result:
employee_name | department_name | project_name |
---|---|---|
Alice | HR | Project X |
Bob | Finance | Project Y |
Charlie | HR | Project Z |
One common question that comes up is the difference between INNER JOIN and LEFT JOIN. Here's a brief comparison:
SELECT employees.employee_name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.department_id;
This query would return all employees, even those without a department, with NULL
for the department_name
if there's no match.
ON
condition (such as employee_id
, department_id
, etc.) are indexed.WHERE
to reduce the amount of data processed.