The Wayback Machine - https://web.archive.org/web/20241111002431/https://www.geeksforgeeks.org/cartesian-join/
Open In App

Cartesian Join

Last Updated : 11 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In SQL, a Cartesian Join is also called a Cross Join, it performs the cartesian product of records of two or more joined tables. A Cartesian product matches each row of one table to every other row of another table, only when the WHERE condition is not specified in the query. In case the WHERE condition is specified then the Cartesian Join works as an Inner Join.

Syntax of Cartesian Join

With the WHERE clause

SELECT *
FROM table 1
CROSS JOIN table 2
WHERE condition

Without the WHERE Clause

SELECT table1.column1 , table2.column2...
FROM table1, table2...
CROSS JOIN table2;

How to Perform Cartesian Join on Tables?

To Perform follow the given steps :

Step 1: To start with first we need to create a Database. For creating a new database use the below command. As an example, we are creating a new database GeeksForGeeks.

Query:

CREATE DATABASE GeeksForGeeks

Output:

Step 1

Step 2: To start working with our database we need to use the database. For this we use the below command.

Query:

USE GeeksForGeeks;

Output:
Step 2

Step 3: Now we need to create tables in this database so as an example we are creating two tables. One is Students and other one is Library. Use the below commands to create the tables.

Query:

CREATE TABLE STUDENTS(
ID INT,
NAME VARCHAR(20),
BRANCH VARCHAR(20),
NUMBER INT);
CREATE TABLE LIBRARY(
BOOK_ID INT,
BOOK_NAME VARCHAR(20),
ISSUED_ON DATE,
DUE_DATE DATE);

Step 3

To view the structure of both tables we use the DESC command.

Query :

DESC STUDENTS;
DESC LIBRARY;

Step 3_1

Step 3_1

Step 4: Now to perform Cartesian Join first we will insert some data in the rows of both STUDENTS and LIBRARY table.

Query:

INSERT INTO STUDENTS VALUES(1,'SURYANSH JOHARI','CS',984012);
INSERT INTO STUDENTS VALUES(2,'AMAN SHARMA','IT',771346);
INSERT INTO STUDENTS VALUES(3,'DEV VERMA','ME',638587);
INSERT INTO STUDENTS VALUES(4,'JOY SMITH','CE',876691);
INSERT INTO STUDENTS VALUES(5,'CHARLES GATTO','EE',997679);
INSERT INTO LIBRARY VALUES(121,'RD SHARMA','2023-01-01','2023-01-08');
INSERT INTO LIBRARY VALUES(236,'GATE CRACKER','2023-02-02','2023-02-09');
INSERT INTO LIBRARY VALUES(352,'MORRIS MANO','2023-03-03','2023-03-10');
INSERT INTO LIBRARY VALUES(970,'NK PUBLICATIONS','2023-04-04','2023-04-11');
INSERT INTO LIBRARY VALUES(648,'BIG BANG THEORY','2023-05-05','2023-05-12');

Output:

Step 4

Step 4

Step 5: After data has been stored in both the tables now we can perform the Cartesian Join on both the tables. As an example here we are doing Cartesian Join with the WHERE clause and finding the records only where NAME of student is Suryansh Johari .

Query:

SELECT *
FROM STUDENTS
CROSS JOIN LIBRARY
WHERE NAME ="SURYANSH JOHARI";

Output:

Step 5

Step 5

Conclusion

In this article, we have learned how to perform Cartesian Join or Cross Join on tables of a SQL database. Cartesian Join is important when there is a need to obtain all the combinations of rows present in the tables of a database. Cartesian Join with WHERE clause behaves as a Inner join and without WHERE clause it simply performs cartesian product of all rows of all the tables as mentioned in the SQL query.

Frequently Asked Questions

1. What is a Cartesian Join?

Cartesian join is a type of join which combines each row of one table to every other row of another table.

2. What happens when a WHERE condition is specified in Cartesian Join?

In case a WHERE condition is specified then Cartesian Join behaves as a Inner Join.

3. What is the difference between Cartesian Join and Natural Join?

Natural Join joins two tables based on same attribute name and datatypes. Cartesian Join produces cartesian product of two tables .



Similar Reads

SQL | Join (Cartesian Join & Self Join)
SQL| JOIN(Inner, Left, Right and Full Joins) In this article, we will discuss about the remaining two JOINS: CARTESIAN JOIN SELF JOIN Consider the two tables below: StudentCourse CARTESIAN JOIN: The CARTESIAN JOIN is also known as CROSS JOIN. In a CARTESIAN JOIN there is a join for each row of one table to every row of another table. This usually h
2 min read
SQL Full Outer Join Using Left and Right Outer Join and Union Clause
An SQL join statement is used to combine rows or information from two or more than two tables on the basis of a common attribute or field. There are basically four types of JOINS in SQL. In this article, we will discuss FULL OUTER JOIN using LEFT OUTER Join, RIGHT OUTER JOIN, and UNION clause. Consider the two tables below: Sample Input Table 1: Pu
3 min read
Difference between Natural join and Inner Join in SQL
The join operation merges the two tables based on the same attribute name and their datatypes are known as Natural join Unlike INNER JOIN, which requires you to specify the columns and conditions for the join explicitly. In this article, we will also see the differences between them. Let's start with Natural Join. Example: If you have two tables "S
3 min read
Left join and Right join in MS SQL Server
Prerequisite – Introduction of MS SQL Server 1. Left Join : A join combines the set of two tables only. A left join is used when a user wants to extract the left table's data only. Left join not only combines the left table's rows but also the rows that match alongside the right table. Syntax - select select_list from table1 left join table2 on joi
2 min read
Self Join and Cross Join in MS SQL Server
Prerequisite - Introduction of MS SQL Server 1. Self Join : Self-join allows us to join a table itself. It is useful when a user wants to compare the data (rows) within the same table. Syntax - select select_list from T t1 [Inner|Left] Join on T t2 on join_predicate. Here T refers to the table we use for comparison and it is referred twice. To avoi
2 min read
SQL | EQUI Join and NON EQUI JOIN
Types of SQL Joins are explained in left, right, and full join and SQL | Join (Cartesian Join & Self Join). And Remaining EQUI Join and NON-EQUI will discuss in this article. Let's discuss one by one. SQL JOINS : EQUI JoinNON-EQUI Join Example - Let's Consider the two tables given below. Table name — Student In this table, you have I'd, name, c
2 min read
Implicit Join vs Explicit Join in SQL
JOIN clause is used to combine rows from two or more tables, based on a relation between them. There are two different syntax forms to perform JOIN operation: Explicit joinImplicit join Step 1: Creating the Database Use the below SQL statement to create a database called geeks: CREATE DATABASE geeks; Step 2: Using the Database Use the below SQL sta
3 min read
Join Multiple Tables Using Inner Join
To retrieve data from the single table we use SELECT and PROJECTION operations but to retrieve data from multiple tables we use JOINS in SQL. There are different types of JOINS in SQL. In this article, only operations on inner joins in MSSQL are discussed. Inner Join is the method of retrieval of data from multiple tables based on a required condit
3 min read
Full join and Inner join in MS SQL Server
Full Join Full join selects all the rows from the left and right tables along with the matching rows as well. If there are no matching rows, it will be displayed as NULL. Syntax: select select_list from table1 full join table2 on join _predicate (OR) select table1.*, table2.* from table1 full join table2 on join _predicate (OR) select * from table1
2 min read
Difference between Inner Join and Outer Join in SQL
JOINS in SQL are fundamental operations used to combine data from multiple tables based on related columns. They are essential for querying data that is distributed across different tables, allowing you to retrieve and present it as a single or similar result set. In this article, We will learn about Inner Join vs Outer Join in SQL in detail. What
5 min read
Difference Between “INNER JOIN” and “OUTER JOIN”
Joins in SQL are essential tools that allow us to combine rows from multiple tables based on specific conditions, often involving a relation between columns in those tables. These joins help in pulling related data together in a meaningful way. Among the most commonly used joins are INNER JOIN and OUTER JOIN. In this article, We will learn about th
5 min read
Difference Between Left Join and Left Outer Join
In SQL language, different joins are used to assemble rows from two or more tables from the related column. The terms "Left Join" and "Left Outer Join" are used interchangeably in SQL but they refer to the same concept. A Left Join retrieves all records from the left table (the first table in the query) and matches them with records from the right
5 min read
Difference Between Left Join and Right Join
In DBMS(Database Management System) Join is an operation that combines the row of two or more tables based on related columns between them. The main purpose of Join is to retrieve the data from multiple tables in other words Join is used to perform multi-table queries. So for that purpose, joins come into the picture. So in this article, we will go
5 min read
Difference Between Right Join and Right Outer Join
Joins in a Database (SQL) are mostly used for combining data or the rows of two or more table records that are based on the same or common attribute. There are various types of Joins like Right Join, Left Join, Full Join, etc. Each join has its own syntax and data-returning capability. In this article, we will see the information about Right Join a
5 min read
SQL Left Outer Join vs Left Join
In SQL, both LEFT JOIN and LEFT OUTER JOIN are used to combine data from two or more tables based on a related column, but they are essentially the same operation, and there is no practical difference between them. The keyword OUTER is optional in most database systems, including popular ones like MySQL, PostgreSQL, and SQL Server. What is a Left J
5 min read
SQL Query to Avoid Cartesian Product
SQL Server is a versatile database. It adheres to the principles of Relational Database Management and hence it is a popular RDBMS. As data is available in multiple tables, if SQL query is not written in an efficient manner, in major scenarios, a Cartesian product occurs. We will see that here. In general Mathematics, a Cartesian product of two set
4 min read
Difference between JOIN and UNION in SQL
Pre-requisites: JOIN, UNION JOIN in SQL is used to combine data from many tables based on a matched condition between them. The data combined using the JOIN statement results in new columns. Consider the two tables: Boys Girls Example: sql> SELECT Boys.Name, Boys.Age, Girls.Address, FROM Boys INNER JOIN Girls ON Boys.Rollno = Girls.Rollno; The r
2 min read
SQL | UPDATE with JOIN
SQL UPDATE JOIN could be used to update one table using another table and join condition. Syntax - UPDATE tablename INNER JOIN tablename ON tablename.columnname = tablename.columnname SET tablenmae.columnnmae = tablenmae.columnname; Use multiple tables in SQL UPDATE with JOIN statement. Let us assume we have two tables - Geeks1 and Geeks2. To check
2 min read
SQL Outer Join
In a relational DBMS, we follow the principles of normalization that allows us to minimize the large tables into small tables. By using a select statement in Joins, we can retrieve the big table back. Outer joins are of following three types. Left outer joinRight outer joinFull outer join Creating a database : Run the following command to create a
3 min read
How to Left Join Multiple Tables in SQL
Left Join is one of the Keywords used while writing queries in SQL. In SQL we normally use Join for the purpose of forming a new table by taking out common data like rows or records or tuples from both the tables which are having matching records in general. Here when it comes to Left Join in SQL it only returns all the records or tuples or rows fr
3 min read
SQL Natural Join
Natural join is an SQL join operation that creates a join on the base of the common columns in the tables. To perform natural join there must be one common attribute(Column) between two tables. Natural join will retrieve from multiple relations. It works in three steps. In this article, we will discuss the overview of SQL Natural Join and then main
3 min read
SQL Full Outer Join Using Union Clause
In this article, we will discuss the overview of SQL, and our main focus will be on how to perform Full Outer Join Using Union Clause in SQL. Let's discuss it one by one. Overview :To manage a relational database, SQL is a Structured Query Language to perform operations like creating, maintaining database tables, retrieving information from the dat
3 min read
SQL Full Outer Join Using Where Clause
A SQL join statement is used to combine rows or information from two or more than two tables on the basis of a common attribute or field. There are basically four types of JOINS in SQL. In this article, we will discuss about FULL OUTER JOIN using WHERE clause. Consider the two tables below: Sample Input Table 1 : PURCHASE INFORMATIONProduct_IDMobil
3 min read
SQL Query to select Data from Tables Using Join and Where
The aim of this article is to make a simple program to Join two tables using Join and Where clause using MySQL. Below is the method to do the same using MySQL. The prerequisites of this article are MySQL and Apache Server on your computer are installed. What is a Query in SQL? A SQL query is a request passed for data/information from a table in a d
3 min read
Difference Between JOIN, IN and EXISTS Clause in SQL
SEQUEL widely known as SQL, Structured Query Language is the most popular standard language to work on databases. We can perform tons of operations using SQL which includes creating a database, storing data in the form of tables, modify, extract and lot more. There are different versions of SQL like MYSQL, PostgreSQL, Oracle, SQL lite, etc. There a
4 min read
How to Join First Row in SQL?
SQL(Structured Query Language) is a powerful tool that is used to manage and manipulate data in a database. Join is a very important tool in SQL that combines multiple tables based on related columns between them. Joining the first row of tables in SQL is a very useful technique when you need to fetch additional information from the tables. In this
5 min read
How Inner Join works in LINQ to SQL
LINQ (Language Integrated Query) in C# provides a powerful way to query data from various data sources, including databases. In LINQ to SQL, which is specifically designed for working with relational databases, an inner join is a common operation used to combine records from two tables based on a specified condition. This article explores the conce
3 min read
SQL DELETE JOIN
DELETE JOIN in SQL lets you delete rows of a table, based on conditions involving another table. We can use the DELETE statement with the JOIN operation to perform DELETE JOIN. We use JOIN to combine data from multiple tables., to delete the same rows or related rows from the table at that time we use delete join. In this article let us see how to
2 min read
SQL CROSS JOIN
SQL CROSS JOIN returns all the records from the left and right tables. CROSS JOIN returns a combination of each row in the left table paired with each row in the right table. CROSS JOIN in SQLCross Join in SQL produces a result set that contains the cartesian product of two or more tables. Cross join is also called a Cartesian Join.  When CROSS JOI
3 min read
SQL Inner Join
SQL INNER JOIN combines two or more tables based on specified columns and retrieves records with matching values in the common columns. INNER JOIN IN SQLThe INNER JOIN clause in SQL is used to combine multiple tables and fetch records that have the same values in the common columns. The difference in the use of INNER JOIN vs OUTER JOIN is that INNE
2 min read
Article Tags :
three90RightbarBannerImg