Table of contents
1.
Introduction
2.
What is Cartesian Product SQL? 
3.
Syntax of Cartesian Product SQL
4.
Cartesian Product SQL Implementation
5.
Example 
5.1.
Implementation To Create Table
5.1.1.
Output
5.2.
Implementation For Adding Values To The Table
5.2.1.
Output
5.3.
Implementation For Cartesian Product
5.3.1.
Output
5.4.
Explanation
6.
Use Cases Of Cartesian Products in SQL
6.1.
Generate All Possible Combinations
6.2.
Create Test Data
6.3.
Data Analysis
7.
Limitation of Using Cartesian Product SQL
8.
Alternative ways to achieve the same result as the Cartesian Product
8.1.
Using Implicit Join
8.2.
Implementation
8.2.1.
Output
8.3.
Using Inner Join:
8.4.
Implementation
8.4.1.
Output
9.
Comparison Between Cartesian Product, Implicit Join and Inner Join.
10.
Frequently Asked Questions
10.1.
What is the difference between Cartesian Product and Inner Join in SQL?
10.2.
Can we perform Cartesian Product between a table and itself in SQL?
10.3.
How can you handle duplicate rows in a Cartesian product?
10.4.
How can you limit the size of the resultant Cartesian product?
10.5.
What is the impact of cartesian products in SQL on Query performance?
11.
Conclusion
Last Updated: Feb 5, 2025
Easy

Exploring the SQL Cartesian Join

Author Vidhi Sareen
0 upvote

Introduction

In SQL, the cartesian product is an operation that combines every row from one table to another. It returns a new table that has values of both tables. The Cartesian product is used when you want to generate every possible combination of both tables. Many real-life examples can be represented as cartesian products, like decks of cards, chess boards, computer images, and many more.

cartesian product sql


This article will be exploring the SQL Cartesian Join in more detail, like their Syntax, usage, and future scope. 

What is Cartesian Product SQL? 

A Cartesian product is a product of two tables whose resultant table consist of all possible and ordered sets. It is useful when you want to combine every row from one table with every row from another table. Order is essential, so we use ordered pairs to obtain a new table from tables A and B.

cartesian product sql

For example, if there are two tables, A and B, each have 3 and 2 lines, respectively. Then the Cartesian product will have 3 * 2 lines. Each row from the first table joins the second row of the second table. Cartesian products involve a large number of redundant operations. It is always recommended to use qualifier operators for large tables.

Also see, Natural Join in SQL

Syntax of Cartesian Product SQL

The following is the syntax of performing a cartesian product in SQL.

SELECT *
FROM table1
CROSS JOIN table2;

 

In the next section, you will get a better understanding of using this syntax.

Cartesian Product SQL Implementation

The Cartesian product is implemented using the CROSS JOIN operator. In some cases, a Cartesian Join acts like an INNER JOIN when the WHERE clause has a significant condition. The basic Syntax that is used is:

SYNTAX

SELECT *
FROM table1
CROSS JOIN table2;


This will create a new table that includes all possible and valid combinations of rows from Table 1 and Table 2.

Let's consider two tables. One table has a list of employees, and another has a list of departments. We will find the cartesian product of the two tables by using the below steps.

Example 

Let's take an example. Create tables named as  ‘employees’ and ‘departments’.

Implementation To Create Table

#create two tables named as employees and departments.
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(50)
);
CREATE TABLE departments (
  id INT PRIMARY KEY,
  name VARCHAR(50)
);

Output

Output

Now insert the values into these tables using the INSERT function in SQL.

Implementation For Adding Values To The Table

#insert values in employees table
INSERT INTO employees (id, name) VALUES
  (1, 'Ninja_1'),
  (2, 'Ninja_2'),
  (3, 'Ninja_3');

#insert values in departments table
INSERT INTO departments (id, name) VALUES
  (1, 'Writer'),
  (2, 'Reviewer');

Output

Employees table:

Output for Employees table

Departments table:

Output for Departments table

 

Now you can perform cartesian products between ‘employees’ and ‘departments’ using SELECT statement and CROSS JOIN function.

Implementation For Cartesian Product

#now display the whole table using SELECT statement
SELECT * FROM employees CROSS JOIN departments;

Output

Output for Cartesian Product

Explanation

  • The first element 'Ninja_1' is taken from Table 1, and the second element, "Writer," is taken from Table 2.
     
  • These elements are multiplied to form an ordered pair as "Ninja_1, Writer".
     
  • The same step is iterated over other elements of the table. This step is repeated until all the possible combinations are not formed.
     
  • The resultant table shows the cartesian product of the elements.

Use Cases Of Cartesian Products in SQL

A Cartesian product in SQL is beneficial when you require all combinations of every row from one table to every row from the other table.

Generate All Possible Combinations

The Cartesian product can generate all possible data combinations from two or more tables. In e-commerce sites, for example, you can generate all viable product and size combinations to create an additional category.

Create Test Data

Cartesian products can be used to generate test data for an application. It will include all possible combinations for testing.

Data Analysis

Cartesian products may be used to determine the link between various types of data.

Limitation of Using Cartesian Product SQL

We are going to discuss some essential limitations regarding Cartesian products in SQL. They are as follows:

  • Cartesian product operations can be expensive, especially when using large data in table form. When two large data tables are combined, the resultant data table would also be enormous, creating problems in handling or performing any operations.
     
  • Cartesian products can create duplicate data, which can cause problems when working with the resulting table.
     
  • Cartesian product operations require a lot of memory, especially when working with large tables. The system does contain that much memory to store, which leads to operation failure.
     
  • Cartesian products are very complex and challenging to understand.

Alternative ways to achieve the same result as the Cartesian Product

There are many alternative ways to achieve the same result as that of cartesian products in SQL. These ways are:

Let's consider the same table as above and demonstrate alternative ways in which we can achieve the same result as that of a Cartesian product.

alternative ways to do  cartesian product

Using Implicit Join

You can use Implicit Join to combine two tables without any specific join condition.This is much equivalent to CROSS JOIN.

Implementation

SELECT *
FROM employees, departments;

Output

Output

Using Inner Join:

You can use INNER JOIN to achieve the same result as of the cartesian product. It is only applicable if the condition is true. It is mostly used in cases where there is a need to combine rows with a specific condition.

Implementation

SELECT *
FROM employees
INNER JOIN departments ON 1=1;

Output

Output

Comparison Between Cartesian Product, Implicit Join and Inner Join.

  Cartesian Product Implicit Join Inner Join
Definition It returns a resultant set which has the combination for every row from one table to every row from another table.joint It is a join that is implied by the relationship between the tables It returns a resultant set that consists of rows that have matching values in both hastables.
Result set The result set has the cartesian product of all the rows from both the tables.result The result set hasresult a combination of all the rows from both tables where the join condition is true. The result set have a combination of only rows from both the tables where the join condition is true.
Performance Very low performance Faster than cartesian product but slower than Inner join Generally fast as compared to other

Also see, Tcl Commands in SQL

Frequently Asked Questions

What is the difference between Cartesian Product and Inner Join in SQL?

Cartesian Product returns all possible combinations from both tables, whereas INNER JOIN returns only the rows that have the same values in both tables based on the JOIN condition applied.

Can we perform Cartesian Product between a table and itself in SQL?

Yes, we can perform Cartesian Product between a table and itself in SQL. This is known as Self-Join or Self- Cartesian Product. We can do this by using the same for the table for both instances in a CROSS JOIN statement.

How can you handle duplicate rows in a Cartesian product?

You can use the DISTINCT or GROUP BY clause to delete some duplicates present in the resultant table of the Cartesian product.

How can you limit the size of the resultant Cartesian product?

The WHERE clause can filter the result based on a specific condition. You can even use the TOP or LIMIT keyword to limit the number of rows the resultant table returns.

What is the impact of cartesian products in SQL on Query performance?

Cartesian products can significantly impact query performance, especially when used for large tables. To optimize it, you can use filter criteria to limit the number of rows or optimize the performance speed.

Conclusion

Cartesian Product is a powerful tool in SQL. It generates all possible combinations of the data from two or more tables. However, it can create some limitations when large data tables are considered. It is essential to use Cartesian Products to perform well by judging all aspects, like their limitations, challenges, and use cases.

In this article, we learn what a Cartesian Product in SQL is. We even discuss implementing Cartesian Products in SQL using an example. We also discuss some use cases and limitations of the Cartesian Product. We also come across other alternative ways in which we can get the same result as that of the Cartesian Product.

To learn more about such content, check out the link below

 

You can find more informative articles or blogs on our platform. You can also practice more coding problems and prepare for interview questions from well-known companies on your platform, Coding Ninjas Studio.

Live masterclass