×

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 Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL RIGHT() Function

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

3 minutes read.

MySQL SELF JOIN

This join links table with itself. The inner join, a self join, a right join, and a cross join are connected with two or more two tables. But, the "self...

4 minutes read.

MySQL Queries

MySQL Queries MySQL supports SQL queries in the MySQL interface. These queries help to interact data with the application. MySQL uses create database, user database, create a table, truncate table, and...

13 minutes read.

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 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 Join

The relational database system needs to interconnect multiple tables with each other. MySQL is a popular and easy data management system to connect multiple tables. The foreign key is used...

5 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.

MySQL logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL DATE_FORMAT() function

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

3 minutes read.

MySQL EQUI JOIN

An equijoin is an operation that combines multiple tables based on equality or matching column values in the associated tables. This operation links more than two tables based on a...

5 minutes read.

MySQL Error 1046 - No Database Selected

What is MySQL? MySQL is a Relational Database Management System (RDBMS). SQL in MySQL is the abbreviation of "Structured Query Language", which Oracle developed it in 1995. It is one of...

1 minute read.

MySQL SOUNDEX() function

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

3 minutes read.

MySQL DATE_ADD() function

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

3 minutes read.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 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.