MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.