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.
General Syntax for performing Cartesian Product on PostgreSQL tables
- SELECT col-list FROM TBL1 CROSS JOIN TBL2;
- SELECT col-list FROM TBL1, TBL2;
- SELECT col-list FROM TBL1 INNER JOIN TBL2 on true;
*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;
SELECT * FROM TBL2;
SELECT TBL1.name , TBL2.category from TBL1 CROSS JOIN TBL2 ;
If you have any question you can leave below in the comment box .