×

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the tables that match the specified condition and hides other rows and columns. MySQL assumes it as a default Join, so it is optional to use the Inner Join keyword with the query.

Syntax

The syntax of the inner join shows below. This syntax works on two tables.

SELECT
Column1 data type constraint,
Column2 data type constraint,
column data type constraint,
FROM table name1
INNER JOIN table2 ON condition1;

The syntax of the inner join shows below. This syntax works on multiple tables.

SELECT
Column1 data type constraint,
Column2 data type constraint,
column data type constraint,
FROM table name1
INNER JOIN table2 ON condition1
INNER JOIN table3 ON condition2;

Examples of the MySQL inner join

Let us understand it with the help of an example. First, we will create two tables in the MySQL system with constraints and a common column.

1) Example: the primary inner join example shows below.

Execute the below query to join two tables. The below statement will return all columns using the "INNER JOIN" query.

mysql> select d.department_name, d.admissions, s.subject_name, s.students
    -> FROM department d
    -> INNER JOIN subject s;

OUTPUT

INNER JOIN

The above image shows all the columns of the two tables. The "department_name", & "admissions" of the department table and "subject_name", & "students" columns of the subject table.

2) Example: the inner join with multiple table examples shows below.

Execute the below query to join two tables. We must specify the condition to join two tables.

mysql> select d.department_name, d.department_id, s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> ON d.department_id = s.subject_id;

OUTPUT

INNER JOIN

The above output image shows the combined result of the two tables. The "department_name" and "department_id" columns have in the department table. The "subject_name" and "students" columns have in the subject table.

3) Example: the inner join with multiple table examples shows below.

Execute the below query to join two tables.

mysql> select *
    -> FROM department d
    -> INNER JOIN subject s
    -> ON d.department_id = s.subject_id;

OUTPUT

INNER JOIN

The above output image shows the combined columns of the two tables. For example, this query shows complete columns of the department and student tables.

4) Example: the inner join with multiple table examples shows below.

Execute the below query to join two tables. For example, this "INNER JOIN" query uses "WHERE" with "AND" conditions.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> WHERE d.department_name = "Bachelor of Technology"
    -> AND  s.subject_name = "Electronics";

OUTPUT

INNER JOIN

This query shows complete columns of the department and student tables. The above image shows the combined columns of the two tables. The inner query works with the logical condition. Both conditions fulfill using "AND" logical condition.

5) Example: the inner join with multiple tables' example shows below.

Execute the below query to join two tables. Two tables join required common columns using conditions. For example, the "INNER JOIN" query uses "WHERE" with "OR" conditions.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> WHERE d.department_name = "Bachelor of Technology"
    -> OR  s.subject_name = "Electronics";

OUTPUT

INNER JOIN

The above image shows the combined columns of the two tables. This query shows complete columns of the department and student tables. The inner query works with the logical condition. The "electronics" column and "bachelor of technology" column display both data rows of the table.

6) Example: the inner join with the "USING" clause example shows below.

Execute the below query to join two tables. For example, the "INNER JOIN" query uses "USING" clause with the "subject_id" foreign key.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> USING (subject_id)

OUTPUT

INNER JOIN

The above output table displays combined columns of the department and subject tables. The "subject_id" column links two tables using a foreign key. The above table is the result of the inner join query with the "USING" clause.

7) Example: the inner join with the "GROUP BY" clause example shows below.

Execute the below query to join two tables. For example, the "INNER JOIN" query uses "GROUP BY" clause with the "subject_id" foreign key. It also uses AND logical operator.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> WHERE d.department_name = "Bachelor of Technology"
    -> AND s.subject_name = "Computer Science"
    -> GROUP BY subject_id;

OUTPUT

INNER JOIN

The above output image displays combined columns of the department and subject tables. The "subject_id" column connects two tables using a foreign key. The above table works inner join query with the "GROUP BY" clause.

8) Example: the inner join with the "ORDER BY" clause example shows below.

Execute the below query to join two tables. For example, the "INNER JOIN" query uses "ORDER BY" and "using" clauses with the "subject_id" foreign key. The inner join uses OR logical operator.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> INNER JOIN subject s
    -> USING (subject_id)
    -> WHERE d.department_name = "Bachelor of Technology"
    -> OR  s.subject_name = "Computer Science"
    -> ORDER BY subject_id DESC;

OUTPUT

INNER JOIN

The above output table displays combined columns of the department and subject tables. It is the result of the inner join query with the "USING" clause. Here, the table applies the inner join query with the "ORDER BY" clause. The table data display in descending order.


Related Topics

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

4 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row...

4 minutes read.

MySQL FROM_BASE64() function

In this context, we will learn how we can use the MySQL FROM_BASE64()() function with proper syntax and good examples. Introduction of MySQL FROM_BASE64()() function Basically, the FROM_BASE64() function In MySQL decodes...

2 minutes read.

MySQL NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.

MySQL Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.

MySQL MONTHNAME() function

In this context, we will learn how we can use the MySQL MONTHNAME() Function with proper syntax and good examples. Introduction of MySQL MONTHNAME() function In MySQL the MONTHNAME() Function is used...

3 minutes read.

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 minutes read.

MySQL vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL ELT() function

In this context, we will learn how we can use the MySQL ELT() function with proper syntax and good examples. Introduction of MySQL ELT() function In the ELT function, the number field...

2 minutes read.

MySQL CURRENT_DATE() Function

In this context, we will learn how we can use the MySQL CURRENT_DATE() function to get the current date, and we will see in the two formats, such as strings...

2 minutes read.

MySQL IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement....

3 minutes read.

MySQL View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.