Oracle Cross Join (Cartesian Products)

The CROSS JOIN specifies that all rows from first table join with all of the rows of second table. If there are “x” rows in table1 and “y” rows in table2 then the cross join result set have x*y rows. It normally happens when no matching join columns are specified.

In simple words you can say that if two tables in a join query have no join condition, then the Oracle returns their Cartesian product.

Syntax

  1. SELECT *   
  2. FROM table1   
  3. CROSS JOIN table2;  

Or

  1. SELECT * FROM table1, table2  

Both the above syntax are same and used for Cartesian product. They provide similar result after execution.

Image representation of cross join

Oracle Cross Join

Oracle Cross Join Example

Let’s take two tables “customer” and “supplier”.

Customer table detail

  1. CREATE TABLE  “CUSTOMER”   
  2.    (    “CUSTOMER_ID” NUMBER,   
  3.     “FIRST_NAME” VARCHAR2(4000),   
  4.     “LAST_NAME” VARCHAR2(4000)  
  5.    )  
  6. /  
Oracle Cross Join 2

Supplier table detail

  1. CREATE TABLE  “SUPPLIER”   
  2.    (    “SUPPLIER_ID” NUMBER,   
  3.     “FIRST_NAME” VARCHAR2(4000),   
  4.     “LAST_NAME” VARCHAR2(4000)  
  5.    )  
  6. /  
Oracle Cross Join 3

Execute this query

  1. SELECT * FROM customer,supplier  

Output

Oracle Cross Join 4

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *