Difference between cross join and natural join in SQL.

 



Difference between cross join and natural join in SQL:

Both of them are used to combine the data from the multiple tables. Its operation is used in database management system.

CROSS JOIN

NATURAL JOIN

It will represent all the possible combination of rows from the two tables. It is also called as cartesian product. It does not require the common column between the tables.

It combines the tables based on the matching column name. Equality may be assumed implicitly between the column of the tables. It eliminates duplicate columns from the results. Matching column name should be same name and data type.

It will return all the rows from both tables where the matching column’s value are equal. If the column with the same name but different data types may cause an issue.

In this, every row of one table is joined with every row of another table.

In this, it is like the join statement which compare the common column of both the tables with each other.

SYNTAX: select * from table1 CROSS JOIN table2;

SYNTAX: select * from table1 NATURAL JOIN table2;

It is more used when the tables have unrelated data.

It is more used when the tables have related data.

If table 1 has 4 rows and table 2 has 5 rows, it will provide result of table to 20 rows. The total number of rows returned is the product of the number of rows in the first table and the number of rows in the second table.

If table 1 has 4 rows and table 2 has 4 rows, it will provide result of table of having common column values match.

 


PRATICAL QUERIES:

select * from emp;

In the emp table,14 records are present.

In the dept table,4 records are present.


Cross join of emp and dept table have 14*4=56 records.



Natural join is performed on both the tables emp and dept,only 14 records are shown as the result which are having deptno 10,20 and 30 respectively. Rows with matching deptno in both tables are displayed.







Comments

Popular posts from this blog

Difference between union and union all in oracle sql plsql

Explain oracle pl sql datatypes interview question and answer for fresher and experienced.

Difference between full join and cross join-oracle sql plsql