CROSS JOIN IN POSTGRESQL

There are multiple join clauses in PostgreSQL, one among them is cross join. Cross join in PostgreSQL is used when we want a Cartesian Product of rows in two or more tables .

What do we mean with Cartesian Product?

Cartesian Product is used to generate all possible combinations by combining rows of the tables. In simple term if we perform Cartesian Product between two tables TBL1 with m rows and TBL2 with n rows then the output will have m*n rows i.e. each row in the first table will be multiplied with all the rows in the second table.

Cross Join

 

General Syntax for performing Cartesian Product on PostgreSQL tables

  1. SELECT col-list FROM TBL1 CROSS JOIN TBL2;
  2. SELECT col-list FROM TBL1, TBL2;
  3. SELECT col-list FROM TBL1 INNER JOIN TBL2 on true;

Note:
*col-list in above queries refer to the list of columns from both the tables that you want in output .

 

CROSS JOIN EXAMPLE

  SELECT * FROM TBL1;

 

cross join

 

SELECT * FROM TBL2; 

Cross Join

SELECT TBL1.name , TBL2.category from TBL1 CROSS JOIN TBL2 ;

 

Cross Join

 

If you have any question you can leave below in the comment box  .

Homepage Link

Leave a Comment

error: Content is protected !!