×

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 is similar to an inner join and a left join.  The natural join does not use "ON" and "USING" clauses with conditions. This join can also use with more than two tables.  The natural join does not need to identify a common column name to join tables. Instead, it includes unique columns automatically.

Rule of the natural join

The following rules always apply to the MySQL natural join.

  • The natural join works similarly as an inner join with the ON clause.
  • The natural join does not use the "ON" clause in the query.
  • The natural join does not use the "USING" clause in the query.

Syntax

The syntax of the natural join shows below.

SELECT
Column1 data type constraint,
Column2 data type constraint,
Column3 data type constraint,
FROM table name1
NATURAL JOIN table2 ON condition1;

It is another syntax of the natural join.

SELECT *FROM table name1
NATURAL JOIN table2 ON condition1;

Examples of the MySQL natural join

1) Example: the NATURAL JOIN with two table's example shows below.

Execute the below query to join two tables. Two tables join uses required columns using similar conditions.

mysql> select d.department_name, d.department_id, 
s.subject_name, s.subject_id
FROM department d
NATURAL JOIN subject s;

OUTPUT

NATURAL JOIN

The above image shows combined columns of the two tables using natural join. The "department_name" and "department_id" columns display in the department table. The "subject_name" and "students" columns display in the subject table.

2) Example: Execute the below natural join query to join two tables. Two tables join with required columns using similar conditions. Here, the query selects the entire columns of the table.

mysql> select *
FROM department d
NATURAL JOIN subject s;

OUTPUT

NATURAL JOIN

The above image shows the entire columns of the department and student tables.

3) Example: Execute the below query to join two tables. Two tables join all columns using the "NATURAL JOIN" query. This query uses the WHERE clause with NATURAL JOIN.

mysql> select d.department_name, d.department_id, s.subject_name, s.subject_id
FROM department d
NATURAL JOIN subject s
WHERE subject_id > 3;

OUTPUT

NATURAL JOIN

The above image shows the required columns of the two tables using natural join. Here, you can see four columns and two rows.

4) Example: Execute the below query to join two tables with the required columns. Two tables join four columns using the "NATURAL JOIN" query. This query uses the "ORDER BY" clause with NATURAL JOIN.

mysql> select d.department_name, d.department_id, s.subject_name, s.subject_id
FROM department d
NATURAL JOIN subject s
ORDER BY 
department_id DESC,
subject_id DESC;

OUTPUT

NATURAL JOIN

The above output image shows the assigned columns of the two tables using natural join. Here, you can see four columns in descending order of data.

5) Example: Execute the below natural join query to join two tables. For example, the "NATURAL JOIN" query uses "WHERE" with "AND" conditions.

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

OUTPUT

NATURAL JOIN

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

6) Example: Execute the below natural join query to join two tables. Two tables join required columns using conditions. For example, the "NATURAL JOIN" query uses "WHERE" with "OR" conditions.

mysql> select d.department_name, d.department_id,
s.subject_name, s.subject_id
FROM department d
NATURAL JOIN subject s
WHERE d.department_name = "Bachelor of Technology"
OR  s.subject_name = "computer science";

OUTPUT

NATURAL JOIN

The above image shows the columns of the two tables. This query shows the required columns of the department table and student table. The cross query works with the logical condition. Both conditions fulfill using the "OR" logical condition.

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

Execute the below query to join two tables. Two tables join required columns using conditions. For example, the "NATURAL JOIN" query uses the "GROUP BY" clause with the "subject_id" foreign key. The natural join uses AND logical operator.

mysql> select d.department_name, d.department_id,
s.subject_name, s.subject_id
FROM department d
NATURAL JOIN subject s
WHERE d.department_name = "Bachelor of Engineering"
GROUP BY subject_id;

OUTPUT

NATURAL 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 natural join query with the "GROUP BY" clause.

8) Example: the natural join with the multiple clause example shows below.

Execute the below query to join two tables. Two tables join required columns using conditions. For example, the "NATURAL JOIN" query uses "ORDER BY" and "using" clauses with the "subject_id" foreign key. The natural join uses OR logical operator.

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

OUTPUT

NATURAL JOIN

The above output table displays combined columns of the department and subject tables. The "subject_id" column connects two tables using a foreign key. The above table works natural join query with the "USING" clause. Here, the table applies a natural join query with the "ORDER BY" clause. The table data display in descending order.


Related Topics

MySQL Tutorial

In this tutorial, you will get information about the MySQL management system. This tutorial will cover the basic and advanced level MySQL concepts with examples that will help you become...

5 minutes read.

MySQL POW() function

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

2 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL ROUND() function

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

3 minutes read.

MySQL CEIL() function

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

3 minutes read.

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 TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

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

MySQL PERIOD_ADD() function

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

2 minutes read.

MySQL CONCAT() function

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

3 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL LOCATE() function

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

3 minutes read.

MySQL PERIOD_DIFF() function

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

2 minutes read.

MySQL PI() function

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

2 minutes read.