Difference between Natural join and Cross join in
SQL
1. Natural Join:
Natural Join joins two tables based on same attribute name and datatypes. The resulting table will
contain all the attributes of both the tables but only one copy of each common column.
Example:
Consider the two tables given below:
Student Table Marks Table
Consider the given query
SELECT *
FROM Student S NATURAL JOIN Marks M;
Output :
2. Cross Join :
Cross Join will produce cross or Cartesian product of two tables if there is no condition specifies.
The resulting table will contain all the attributes of both the tables including duplicate or common
columns also.
Example:
Consider the above two tables and the query is given below:
SELECT *
FROM Student S CROSS JOIN Marks M;
Output:
Difference between Natural JOIN and CROSS JOIN in SQL
SR.NO. NATURAL JOIN CROSS JOIN
Natural Join joins two tables
based on same attribute name Cross Join will produce cross or
1. and datatypes. cartesian product of two tables .
In Natural Join, The resulting In Cross Join, The resulting
table will contain all the table will contain all the
attributes of both the tables but attribute of both the tables
keep only one copy of each including duplicate columns
2. common column also
In Cross Join, If there is no
condition specifies then it
In Natural Join, If there is no returns all possible pairing of
condition specifies then it rows from both the tables
returns the rows based on the whether they are matched or
3. common column unmatched
SYNTAX: SYNTAX:
SELECT * FROM table1 SELECT * FROM table1
4. NATURAL JOIN table2; CROSS JOIN table2;
What is Equi Join in SQL?
SQL EQUI JOIN performs a JOIN against equality or matching column(s) values of the
associated tables. An equal sign (=) is used as comparison operator in the where clause to refer
equality.
You may also perform EQUI JOIN by using JOIN keyword followed by ON keyword and then
specifying names of the columns along with their associated tables to check equality.
Pictorial presentation of SQL Equi Join:
Syntax:
or
Example:
Here is an example of Equi Join in SQL.
Sample table: agents
Sample table: customer
To get agent name column from agents table and cust name and cust city columns from customer
table after joining said two tables with the following condition -
1. working area of agents and customer city of customer table must be same,
the following SQL statement can be used:
SQL Code:
SELECT agents.agent_name,customer.cust_name,
customer.cust_city
FROM agents,customer
WHERE agents.working_area=customer.cust_city;
Copy
Output:
What is Natural Join in SQL?
We have already learned that an EQUI JOIN performs a JOIN against equality or matching
column(s) values of the associated tables and an equal sign (=) is used as comparison operator in
the where clause to refer equality.
The SQL NATURAL JOIN is a type of EQUI JOIN and is structured in such a way that, columns
with the same name of associated tables will appear once only.
Pictorial presentation of the above SQL Natural Join:
Natural Join: Guidelines
- The associated tables have one or more pairs of identically named columns.
- The columns must be the same data type.
- Don’t use ON clause in a natural join.
Syntax:
Example:
Here is an example of SQL natural join between two tables:
Sample table: foods
Sample table: company
To get all the unique columns from foods and company tables, the following SQL statement can
be used:
SQL Code:
SELECT *
FROM foods
NATURAL JOIN company;
Output
What is Cross Join in SQL?
The SQL CROSS JOIN produces a result set which is the number of rows in the first table
multiplied by the number of rows in the second table if no WHERE clause is used along with
CROSS JOIN. This kind of result is called as Cartesian Product.
Syntax:
SELECT *
FROM table1
CROSS JOIN table2;
Alternative Syntax:
SELECT *
FROM table1, table2;
Use a CROSS JOIN to generate combinations of all rows between two tables for analysis
or when we need to perform operations on every combination of records.
Consider the Cross join between company and foods:
How cross joining happend into two tables