×

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 CEILING() function

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

2 minutes read.

MySQL FORMAT() function

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

2 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 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 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 HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL RIGHT JOIN

MySQL right join links two tables with each other. The left table column connects with the complete right side table. Each row of the right table tries to connect with...

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

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 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 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 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 Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 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 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 database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

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