0% found this document useful (0 votes)
33 views8 pages

SQL INNER JOIN: The Beginner's Guide To Inner Join in SQL

This document provides an introduction to SQL INNER JOIN, explaining that it links two or more tables by matching column values. It gives examples of joining two tables and three tables, showing how to qualify column names when tables share column names. Screenshots demonstrate the JOIN clauses in action.

Uploaded by

Mama Jasmine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views8 pages

SQL INNER JOIN: The Beginner's Guide To Inner Join in SQL

This document provides an introduction to SQL INNER JOIN, explaining that it links two or more tables by matching column values. It gives examples of joining two tables and three tables, showing how to qualify column names when tables share column names. Screenshots demonstrate the JOIN clauses in action.

Uploaded by

Mama Jasmine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SQL INNER JOIN: The Beginner's Guide to Inner Join in SQL about:reader?url=https://www.sqltutorial.

org/sql-inner-join/

sqltutorial.org

SQL INNER JOIN: The Beginner's


Guide to Inner Join in SQL
5-6 menit

Summary: in this tutorial, we will show you how to use the SQL
INNER JOIN clause to query data from two or more tables.

Introduction to the SQL INNER JOIN clause

So far, you have learned how to use the SELECT statement to


query data from a single table. However, the SELECT statement is
not limited to query data from a single table. The SELECT
statement can link multiple tables together.

The process of linking tables is called joining. SQL provides many


kinds of joins such as inner join, left join, right join, full outer join,
etc. This tutorial focuses on the inner join.

The inner join clause links two (or more) tables by a relationship
between two columns. Whenever you use the inner join clause,
you normally think about the intersection.

It is much easier to understand the inner join concept through a


simple example.

Suppose, we have two tables: A & B.

Table A has four rows: (1,2,3,4) and table B has four rows:

1 dari 8 27/02/2021 08.23


SQL INNER JOIN: The Beginner's Guide to Inner Join in SQL about:reader?url=https://www.sqltutorial.org/sql-inner-join/

(3,4,5,6)

When table A joins with the table B using the inner join, we have
the result set (3,4) that is the intersection of the table A and table
B.

See the following picture.

SQL INNER JOIN

For each row in table A, the inner join clause finds the matching
rows in the table B. If a row is matched, it is included in the final
result set.

Suppose the column name of the A & B tables is n, the following


statement illustrates the inner join clause:

SELECT
A.n
FROM A
INNER JOIN B ON B.n = A.n;

The INNER JOIN clause appears after the FROM clause. The
condition to match between table A and table B is specified after
the ON keyword. This condition is called join condition i.e., B.n =
A.n

The INNER JOIN clause can join three or more tables as long as
they have relationships, typically foreign key relationships.

For example, the following statement illustrates how to join 3

2 dari 8 27/02/2021 08.23


SQL INNER JOIN: The Beginner's Guide to Inner Join in SQL about:reader?url=https://www.sqltutorial.org/sql-inner-join/

tables: A, B, and C:

SELECT
A.n
FROM A
INNER JOIN B ON B.n = A.n
INNER JOIN C ON C.n = A.n;

SQL INNER JOIN examples

SQL INNER JOIN 2 tables example

We will use the employees and departments table to


demonstrates how the INNER JOIN clause works.

emp_dept_tables

Each employee belongs to one and only one department while


each department can have more than one employee. The
relationship between the employees and departments table is one-
to-many.

The department_id column in the employees table is the


foreign key column that links the employees to the
departments table.

3 dari 8 27/02/2021 08.23


SQL INNER JOIN: The Beginner's Guide to Inner Join in SQL about:reader?url=https://www.sqltutorial.org/sql-inner-join/

To get the information of the department id 1,2, and 3, you use the
following statement.

SELECT
department_id,
department_name
FROM
departments
WHERE
department_id IN (1, 2, 3);

See it in action

SQL INNER JOIN


departments table

Notice that we used the IN operator in the WHERE clause to get


rows with department_id 1, 2 and 3.

To get the information of employees who work in the department id


1, 2 and 3, you use the following query:

SELECT
first_name,
last_name,
department_id
FROM
employees
WHERE
department_id IN (1, 2, 3)
ORDER BY
department_id;

See it in action

4 dari 8 27/02/2021 08.23


SQL INNER JOIN: The Beginner's Guide to Inner Join in SQL about:reader?url=https://www.sqltutorial.org/sql-inner-join/

SQL INNER JOIN


employees table

To combine data from these two tables, you use an inner join
clause as the following query:

SELECT
first_name,
last_name,
employees.department_id,
departments.department_id,
department_name
FROM
employees
INNER JOIN
departments ON departments.department_id =
employees.department_id
WHERE
employees.department_id IN (1 , 2, 3);

See it in action

SQL INNER JOIN example

5 dari 8 27/02/2021 08.23


SQL INNER JOIN: The Beginner's Guide to Inner Join in SQL about:reader?url=https://www.sqltutorial.org/sql-inner-join/

For each row in the employees table, the statement checks if the
value of the department_id column equals the value of the
department_id column in the departments table. If the
condition

If the condition employees.department_id =


departments.department_id is satisfied, the combined row
that includes data from rows in both employees and
departments tables are included in the result set.

Notice that both employees and departments tables have the


same column name department_id, therefore we had to qualify
the department_id column using the syntax
table_name.column_name.

SQL INNER JOIN 3 tables example

Each employee holds one job while a job may be held by many
employees. The relationship between the jobs table and the
employees table is one-to-many.

The following database diagram illustrates the relationships


between employees, departments and jobs tables:

emp_dept_jobs_tables

6 dari 8 27/02/2021 08.23


SQL INNER JOIN: The Beginner's Guide to Inner Join in SQL about:reader?url=https://www.sqltutorial.org/sql-inner-join/

The following query uses the inner join clauses to join 3 tables:
employees, departments, and jobs to get the first name, last name,
job title, and department name of employees who work in
department id 1, 2, and 3.

SELECT
first_name,
last_name,
job_title,
department_name
FROM
employees e
INNER JOIN departments d ON d.department_id =
e.department_id
INNER JOIN jobs j ON j.job_id = e.job_id
WHERE
e.department_id IN (1, 2, 3);

See it in action

SQL INNER JOIN 3 tables example

Now you should understand how the SQL INNER JOIN clause
works and know how to apply it to query data from multiple tables.

Was this tutorial helpful ?

7 dari 8 27/02/2021 08.23


SQL INNER JOIN: The Beginner's Guide to Inner Join in SQL about:reader?url=https://www.sqltutorial.org/sql-inner-join/

YesNo

8 dari 8 27/02/2021 08.23

You might also like