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

SELECT *   

FROM table1   

CROSS JOIN table2;  

    Or

    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

      CREATE TABLE  "CUSTOMER"   
    
       (    "CUSTOMER_ID" NUMBER,   
    
        "FIRST_NAME" VARCHAR2(4000),   
    
        "LAST_NAME" VARCHAR2(4000)  
    
       )  
    
    /  
      Oracle Cross Join 2

      Supplier table detail

        CREATE TABLE  "SUPPLIER"   
      
         (    "SUPPLIER_ID" NUMBER,   
      
          "FIRST_NAME" VARCHAR2(4000),   
      
          "LAST_NAME" VARCHAR2(4000)  
      
         )  
      
      /  
        Oracle Cross Join 3

        Execute this query

        SELECT * FROM customer,supplier  

        Output

        Oracle Cross Join 4

        Comments

        Leave a Reply

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