Thursday, July 26, 2012

Cartesian Product in SQL

Cartesian Products: If two tables in a join query have no join condition, Oracle returns their Cartesian product. Oracle combines each row of one table with each row of the other. A Cartesian product always generates many rows and is rarely useful.A Cartesian product is formed when:A join condition is omittedA join condition is invalidAll rows in the first table are joined to all rows in the second table
To avoid a Cartesian product, always include a valid join condition in a WHERE clause.

Example-
we have two table emp and dept in scott user
emp have 14 rows and dept have 5 rows
if we write this query
select emp.ename,emp.sal,dept.dname from emp,dept;
this query provide you 70 rows
this called cartesian product 14*5

No comments:

Post a Comment