Worldscope

PostgreSQL Cross Join

Palavras-chave:

Publicado em: 05/08/2025

PostgreSQL CROSS JOIN: A Comprehensive Guide

The PostgreSQL `CROSS JOIN` returns a Cartesian product of rows from the joined tables. This means it combines each row from the first table with every row from the second table. This article provides a detailed explanation and examples of using the `CROSS JOIN` in PostgreSQL.

Fundamental Concepts / Prerequisites

Before diving into `CROSS JOIN`, it's helpful to have a basic understanding of:

  • SQL: Familiarity with SQL syntax, including `SELECT`, `FROM`, and `JOIN` clauses.
  • Tables and Rows: A general understanding of relational database concepts, including tables, columns, and rows.

Essentially, you should be comfortable with querying data from a database using basic SQL.

Core Implementation: PostgreSQL CROSS JOIN


-- Create two sample tables
CREATE TABLE employees (
    employee_id SERIAL PRIMARY KEY,
    employee_name VARCHAR(50)
);

CREATE TABLE departments (
    department_id SERIAL PRIMARY KEY,
    department_name VARCHAR(50)
);

-- Insert some data into the tables
INSERT INTO employees (employee_name) VALUES
('Alice'),
('Bob'),
('Charlie');

INSERT INTO departments (department_name) VALUES
('Sales'),
('Marketing');

-- Perform a CROSS JOIN
SELECT
    e.employee_name,
    d.department_name
FROM
    employees e
CROSS JOIN
    departments d;

-- Sample Output:
-- employee_name | department_name
-- ---------------|-----------------
-- Alice         | Sales
-- Alice         | Marketing
-- Bob           | Sales
-- Bob           | Marketing
-- Charlie       | Sales
-- Charlie       | Marketing

Code Explanation

The code begins by creating two tables, `employees` and `departments`, each with an `id` and `name` column. The `employee_id` and `department_id` columns are set as primary keys. `SERIAL` data type automatically creates sequence to make sure the id is unique and incremental.

Data is then inserted into both tables. The `employees` table contains three employee names, and the `departments` table contains two department names.

Finally, the `CROSS JOIN` operation is performed. The `SELECT` statement retrieves the `employee_name` from the `employees` table (aliased as `e`) and the `department_name` from the `departments` table (aliased as `d`). The `FROM` clause specifies the tables to join, and the `CROSS JOIN` keyword indicates that we want a Cartesian product of the rows in the two tables.

The result is a table where each employee is paired with each department.

Analysis

Complexity Analysis

The time complexity of a `CROSS JOIN` is O(N*M), where N is the number of rows in the first table and M is the number of rows in the second table. This is because every row in the first table is combined with every row in the second table.

The space complexity is also O(N*M), as the resulting table contains N*M rows. This is because the result set needs to store all possible combinations of rows from both tables.

Alternative Approaches

While `CROSS JOIN` is the standard way to achieve a Cartesian product, it can also be achieved implicitly by listing tables in the `FROM` clause without a `JOIN` condition. For example:


SELECT
    e.employee_name,
    d.department_name
FROM
    employees e,
    departments d;

This query achieves the same result as the `CROSS JOIN`, but using the explicit `CROSS JOIN` keyword is generally considered more readable and less prone to errors, as it clearly expresses the intention to perform a Cartesian product. Many linters would suggest using `CROSS JOIN` for readability purposes.

Conclusion

The PostgreSQL `CROSS JOIN` is a powerful tool for generating all possible combinations of rows from two tables. It's important to understand its time and space complexity, as it can quickly lead to large result sets if the tables being joined are large. While implicit Cartesian products are possible, using the explicit `CROSS JOIN` syntax promotes readability and clarity in your SQL queries.