PostgreSQL Cross Join
Summary: in this tutorial, you will learn how to use the PostgreSQL CROSS JOIN
to produce a cartesian product of rows from the joined tables.
Introduction to the PostgreSQL CROSS JOIN clause
In PostgreSQL, a cross-join allows you to join two tables by combining each row from the first table with every row from the second table, resulting in a complete combination of all rows.
In the set theory, we can say that a cross-join produces the cartesian product of rows in two tables.
Unlike other join clauses such as LEFT JOIN or INNER JOIN, the CROSS JOIN
clause does not have a join predicate.
Suppose you have to perform a CROSS JOIN
of table1
and table2
.
If table1
has n
rows and table2
has m
rows, the CROSS JOIN
will return a result set that has nxm
rows.
For example, the table1
has 1,000
rows and table2
has 1,000
rows, the result set will have 1,000 x 1,000
= 1,000,000
rows.
Because a CROSS JOIN
may generate a large result set, you should use it carefully to avoid performance issues.
Here’s the basic syntax of the CROSS JOIN
syntax:
The following statement is equivalent to the above statement:
Alternatively, you can use an INNER JOIN
clause with a condition that always evaluates to true to simulate a cross-join:
PostgreSQL CROSS JOIN example
The following CREATE TABLE statements create T1
and T2
tables and insert sample data for the cross-join demonstration.
The following statement uses the CROSS JOIN
operator to join T1
table with T2
table:
The following picture illustrates how the CROSS JOIN
works when joining the T1
table with the T2
table:
Some practical examples of using CROSS JOIN
In practice, you can find the CROSS JOIN
useful when you need to combine data from two tables without specific matching conditions. For example:
1) Scheduling
Suppose you have a table for employees
and shifts
, and you want to create a schedule that lists all possible combinations of employees and shifts to explore various staffing scenarios:
2) Inventory management
In an inventory management system, you have tables for warehouses
and products
. A CROSS JOIN
can help you analyze the availability of each product in every warehouse:
Summary
- Use the PostgreSQL
CROSS JOIN
clause to make a cartesian product of rows in two tables.