CROSS JOIN
producing a Cartesian product.
Overview
CROSS JOIN pairs every left-side row with every right-side row, so the result size is the product of both inputs. It is useful for complete grids such as date × region or product × option, but grows rapidly on large inputs. If the intent is key-based matching, a missing ON condition is usually a defect.
Basic syntax
SELECT a.name, b.name FROM table_a a CROSS JOIN table_b b;
Examples
CROSS JOIN example
The example combines two rows from table_a with two rows from table_b to produce four rows, making the Cartesian product explicit.
Sample table
table_a| name |
|---|
| A |
| B |
table_b| name |
|---|
| X |
| Y |
SQL
SELECT a.name AS a_name, b.name AS b_name FROM table_a a CROSS JOIN table_b b;
Run result
| a_name | b_name |
|---|---|
| A | X |
| A | Y |
| B | X |
| B | Y |
Common issues
Without a later filter, row count is the product of both inputs; 100 by 1,000 rows produces 100,000 rows.
It is useful for complete date-store grids, test data, or all candidate combinations, but estimate the result size first.
Filter both inputs in subqueries or CTEs, select only necessary columns, and validate estimated row counts on sample data.
Tags
Was this explanation clear?
Thanks. This feedback will inform the next content review.