In this medium post, we will learn about Cartesian Joins in SQL.
Learning Objectives:
- Define Cartesian joins.
- Describe some cases where Cartesian joins might be helpful.
- Write appropriate SQL to establish a Cartesian join.
One important thing to consider is that Cartesian joins are computationally expensive. If you have two tables with 10 rows each, you will run 100 queries to achieve a cartesian join.
Cartesian (Cross) Join Example.
Here is an example.
SELECT product_name,
unit_price,
company_name
FROM suppliers CROSS JOIN products
Notice that table 1 is called suppliers and table 2 is called products.
Let's say that table 1 has 23 records and table 2 has 33 records. This means that there will be 759 rows once the query has finished executing.
There is also no guarantee that the columns you want will match any particular value.
This is why this particular join is rarely used and why caution is advised.