In SQL, a CROSS JOIN is a type of join that returns the Cartesian product of two tables. This means that every row from the first table is combined with every row from the second table, regardless of any relationship between the tables. Unlike other joins, there is no condition to match rows. A CROSS JOIN can lead to a large number of results, especially when both tables have a lot of rows.
A CROSS JOIN combines all rows from the first table with all rows from the second table. It is also known as a Cartesian product because it produces a result set that represents all possible combinations of rows from both tables.
A
has m
rows and table B
has n
rows, the result will contain m * n
rows.The syntax for using CROSS JOIN is simple and straightforward:
SELECT columns
FROM table1
CROSS JOIN table2;
Let’s understand how CROSS JOIN works with an example. We’ll use two simple tables: products
and colors
.
-- products table
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 1 | T-Shirt |
| 2 | Hat |
+------------+--------------+
-- colors table
+--------+--------+
| color | color_id |
+--------+--------+
| Red | 1 |
| Blue | 2 |
+--------+--------+
SELECT products.product_name, colors.color
FROM products
CROSS JOIN colors;
Explanation:
product_name
from the products
table and the color
from the colors
table.Result:
product_name | color |
---|---|
T-Shirt | Red |
T-Shirt | Blue |
Hat | Red |
Hat | Blue |
Explanation of Result:
T-Shirt
is listed with both Red
and Blue
colors, and similarly, the Hat
is also listed with both colors.While CROSS JOIN is less commonly used compared to other types of joins, it can be very useful in specific scenarios. Here are a few use cases:
If you need to generate all possible combinations of two sets of data, a CROSS JOIN is perfect. For example, generating all possible combinations of product sizes and colors.
In scenarios where you need to analyze or test all possible combinations of data, a CROSS JOIN can help you quickly generate large datasets for experimentation.
A CROSS JOIN can be used in reporting systems where you need to calculate every combination of data from different sources, such as generating a report for every combination of products and regions.
Because CROSS JOIN generates a Cartesian product, it can lead to a very large result set, especially when the tables involved contain many rows. Here are some performance considerations to keep in mind:
A
has 1,000 rows and table B
has 1,000 rows, the result will contain 1,000,000 rows.WHERE
clauses to filter the result set and reduce the number of rows returned.You might be wondering how CROSS JOIN compares with INNER JOIN. While both can combine rows from two tables, there are significant differences:
SELECT products.product_name, colors.color
FROM products
INNER JOIN colors
ON products.product_id = colors.color_id;
Result for INNER JOIN:
product_name | color |
---|---|
T-Shirt | Red |
Hat | Blue |
SELECT products.product_name, colors.color
FROM products
CROSS JOIN colors;
Result for CROSS JOIN:
product_name | color |
---|---|
T-Shirt | Red |
T-Shirt | Blue |
Hat | Red |
Hat | Blue |
As shown, INNER JOIN only returns rows where there is a match between the tables, while CROSS JOIN returns every possible combination of rows.
Since CROSS JOIN produces a Cartesian product, it’s crucial to be careful when using it, especially with large tables. To avoid creating enormous result sets by mistake, you can:
LIMIT
or TOP
(depending on your database system).
SELECT products.product_name, colors.color
FROM (SELECT * FROM products LIMIT 2) AS products
CROSS JOIN (SELECT * FROM colors LIMIT 2) AS colors;
Result:
product_name | color |
---|---|
T-Shirt | Red |
T-Shirt | Blue |
Hat | Red |
Hat | Blue |
This query limits the number of rows from both tables before performing the CROSS JOIN, ensuring the result set is smaller.