Skip to content

SQL - Cross Join

A SQL CROSS JOIN is a type of join that returns the Cartesian product of rows from the tables in the join. In simpler terms, it combines every row from the first table with every row from the second table. This operation results in a table that contains all possible pairings of rows.

Imagine you have a set of shirts and a set of pants. A Cartesian product is like creating every possible outfit by pairing each shirt with every single pair of pants. If you have 3 shirts and 4 pairs of pants, you’ll end up with 3 * 4 = 12 different outfits.

Warning: Because CROSS JOIN generates M x N rows (where M is the number of rows in the first table and N in the second), it can produce extremely large result sets very quickly. Using it on large tables can lead to severe performance issues or even server crashes. It is often the result of an accidental omission of a JOIN condition in a query.

The basic syntax for a CROSS JOIN is explicit and clear:

SELECT column_list
FROM table1
CROSS JOIN table2;

An older, implicit syntax also exists, but it is not recommended as it’s less readable and can be mistaken for an error:

-- This syntax also produces a CROSS JOIN but is less clear.
SELECT column_list
FROM table1, table2;

Despite the warnings, CROSS JOIN has valid use cases. It’s particularly useful for generating combination data.

Let’s say we have a products table and a colors table. We want to generate a list of every product in every available color to populate our inventory.

-- First, let's create our tables with modern data types.
CREATE TABLE products (
product_id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(255) NOT NULL
);
CREATE TABLE colors (
color_id INT PRIMARY KEY AUTO_INCREMENT,
color_name VARCHAR(50) NOT NULL
);
-- Now, insert some sample data.
INSERT INTO products (product_name) VALUES
('Classic T-Shirt'),
('Hoodie');
INSERT INTO colors (color_name) VALUES
('Red'),
('Blue'),
('Black');

Now, we can use CROSS JOIN to generate all possible variants:

SELECT
p.product_name,
c.color_name
FROM
products AS p
CROSS JOIN
colors AS c
ORDER BY
p.product_name, c.color_name;

The query produces a complete list of all 2 * 3 = 6 combinations:

product_namecolor_name
Classic T-ShirtBlack
Classic T-ShirtBlue
Classic T-ShirtRed
HoodieBlack
HoodieBlue
HoodieRed

You can chain CROSS JOINs to combine more than two tables. The number of rows in the result will be the product of the row counts of all tables involved.

SELECT column_list
FROM table1
CROSS JOIN table2
CROSS JOIN table3
...;

Let’s add a sizes table to our previous example to generate a full inventory list.

-- Create and populate the sizes table
CREATE TABLE sizes (
size_id INT PRIMARY KEY AUTO_INCREMENT,
size_code VARCHAR(10) NOT NULL
);
INSERT INTO sizes (size_code) VALUES ('S'), ('M'), ('L'), ('XL');

Now, we join all three tables:

SELECT
p.product_name,
c.color_name,
s.size_code
FROM
products AS p
CROSS JOIN
colors AS c
CROSS JOIN
sizes AS s
ORDER BY
p.product_name, c.color_name, s.size_code;

This query generates all 2 (products) * 3 (colors) * 4 (sizes) = 24 possible combinations. Here’s a small sample of the result:

product_namecolor_namesize_code
Classic T-ShirtBlackL
Classic T-ShirtBlackM
Classic T-ShirtBlackS
Classic T-ShirtBlackXL
Classic T-ShirtBlueL
………
  • Accidental CROSS JOIN: The most common mistake is forgetting a join condition (e.g., ON t1.id = t2.t1_id or a WHERE clause). Always use explicit JOIN syntax (INNER JOIN, LEFT JOIN, etc.) to avoid this.
  • Performance: Be extremely cautious when using CROSS JOIN on tables that are not small. Always analyze the potential size of the result set before running the query.
  • Use with a WHERE clause: Sometimes a CROSS JOIN is used to generate a set of potential combinations, which is then filtered down by a WHERE clause. This can be a powerful technique for solving complex problems.