Buscar en moleculax
PostgreSQL Cross Join By Example

Introduction to the PostgreSQL CROSS JOIN clause

CROSS JOIN clause allows you to produce the Cartesian Product of rows in two or more tables. Different from the other JOIN operators such as LEFT JOIN  or INNER JOIN, the CROSS JOIN does not have any matching condition in the join clause.
Suppose we have to perform the CROSS JOIN of two tables T1 and T2. For every row from T1 and T2 i.e., a cartesian product, the result set will contain a row that consists of all columns in the T1 table followed by all columns in the T2 table. If T1 has N rows, T2 has M rows, the result set will have N x M rows.
The following illustrates the syntax of the PostgreSQL CROSS JOIN clause:
The following statement is also equivalent to the CROSS JOIN above:
You can use the INNER JOIN clause with the condition evaluates to true to perform the cross join as follows:

PostgreSQL CROSS JOIN example

The following CREATE TABLE statements create T1 and T2 tables and insert some sample data for the cross demonstration.
The following statement uses the CROSS JOIN operator to join the T1 table with the T2 table.
The following picture illustrates the result of the CROSS JOIN operator when we join the T1 table with the T2 table:


In this tutorial, you have learned how to use the PostgreSQL CROSS JOIN clause to make a Cartesian Product of rows in two or more tables.

.


Powered by

http://emiliogomez.com.ar