You are on page 1of 17

Cartesian Product Operation

Submitted by :

NAME : SUSHREYA GHOSH

ECE SECTION : A

ROLL NUMBER : 13000317030 TECHNO MAIN SALT LAKE

SUBJECT : DATABASE MANAGEMENT SYSTEM

SUBJECT CODE : EC 705C


CONTENTS
● INTRODUCTION
● CARTESIAN OR CROSS PRODUCT
★ EXAMPLE
● CHARACTERISTICS OF CARTESIAN PRODUCT
● CARTESIAN JOIN OR CROSS JOIN
★ EXAMPLE 1
★ EXAMPLE 2
★ EXAMPLE 3
● SUMMARY
● REFERENCES
INTRODUCTION
● Relational Algebra is a widely used procedural query language. It
collects instances of relations as input and gives occurrences of relations
as output.
● It uses various operations to perform this action. The output of these
operations is a new relation, which might be formed from one or more
input relations.
● Cartesian Product Operation is one such operation in Relational
Algebra.
● Cartesian Product in DBMS is an operation used to merge columns
from two relations.
● Cartesian Product Operation is also called Cross Product or Cross
Join.
CARTESIAN OR CROSS PRODUCT
● CARTESIAN OR CROSS PRODUCT between two relations let say A and B,
so cross product between A X B will results all the attributes of A
followed by each attribute of B. Each record of A will pairs with every
record of B.
● Relations are nothing but a set of tuples, and in Cartesian Product
Operation we will need 2 sets of tuples.
● CROSS PRODUCT is a binary set operation means, at a time we can apply
the operation on two relations. But the two relations on which we are
performing the operations do not have the same type of tuples, which
means Union compatibility (or Type compatibility) of the two relations is
not necessary.
On applying CARTESIAN PRODUCT on two relations that is on two sets of
tuples, it will take every tuple one by one from the left set(relation) and will pair it
up with all the tuples in the right set(relation).
So, the CROSS PRODUCT of two relation A(R1, R2, R3, …, Rp) with degree p,
and B(S1, S2, S3, …, Sn) with degree n, is a relation C(R1, R2, R3, …, Rp, S1,
S2, S3, …, Sn) with degree p + n attributes.

Notation:

A✕S

where A and S are the relations,


the symbol ‘✕’ is used to denote the CROSS PRODUCT operator.
EXAMPLE:

Consider two relations STUDENT (SNO, FNAME, LNAME) and DETAIL (ROLLNO,
AGE) below:

STUDENT

SNOFNAME LNAME
1 Albert D’Souza
2 Esha Fatehi

DETAIL

ROLLNOAGE
5 18
9 21
On applying CROSS PRODUCT on STUDENT and DETAIL:
STUDENT ✕ DETAILS

SNO FNAME LNAME ROLLNOAGE


1 Albert D’Souza 5 18
1 Albert D’Souza 9 21
2 Esha Fatehi 5 18
2 Esha Fatehi 9 21

We can observe that the number of tuples in STUDENT relation is 2, and the
number of tuples in DETAIL is 2. So the number of tuples in the resulting relation on
performing CROSS PRODUCT is 2*2 = 4.
CHARACTERISTICS OF CARTESIAN PRODUCT

1. The cardinality (number of tuples) of resulting relation from a Cross Product


operation is equal to the number of attributes(say m) in the first relation
multiplied by the number of attributes in the second relation(say n).
Cardinality = m*n
2. The Cross Product of two relation A(R1, R2, R3, …, Rp) with degree p, and B(S1, S2,
S3, …, Sn) with degree n, is a relation C(R1, R2, R3, …, Rp, S1, S2, S3, …, Sn) with
degree p + n attributes.
Degree = p+n

3. In SQL CARTESIAN PRODUCT(CROSS PRODUCT) can be applied using CROSS JOIN.


4. In general, we don’t use cartesian Product unnecessarily, which means without
proper meaning we don’t use Cartesian Product. Generally, we use Cartesian
Product followed by a Selection operation and comparison on the operators
CARTESIAN JOIN or CROSS JOIN
The CARTESIAN JOIN or CROSS JOIN returns the Cartesian product of
the sets of records from two or more joined tables. Thus, it equates to an inner
join where the join-condition always evaluates to either True or where the
join-condition is absent from the statement

Syntax

The basic syntax of the CARTESIAN JOIN or the CROSS JOIN is as


follows −

SELECT table1.column1, table2.column2...


FROM table1, table2 [, table3 ]
Example 1 :
Consider the following two tables.
Table 1 − CUSTOMERS table is as follows.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
+----+----------+-----+-----------+----------+
Table 2: ORDERS Table is as follows −
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
Now, let us join these two tables using CARTESIAN JOIN as follows - SQL> SELECT ID, NAME, AMOUNT, DATE

FROM CUSTOMERS, ORDERS;

This would produce the following result −


+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 1 | Ramesh | 3000 | 2009-10-08 00:00:00 |
| 1 | Ramesh | 1500 | 2009-10-08 00:00:00 |
| 1 | Ramesh | 1560 | 2009-11-20 00:00:00 |
| 1 | Ramesh | 2060 | 2008-05-20 00:00:00 |
| 2 | Khilan | 3000 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 2 | Khilan | 2060 | 2008-05-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 2060 | 2008-05-20 00:00:00 |
| 4 | Chaitali | 3000 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
EXAMPLE 2 :
EMPLOYEE

EMP_ID EMP_NAME EMP_DEPT


1 Smith A

2 Harry C

3 John B

DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing

B Sales

C Legal
Input:

EMPLOYEE X DEPARTMENT

Output:

EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME


1 Smith A A Marketing

1 Smith A B Sales

1 Smith A C Legal

2 Harry C A Marketing

2 Harry C B Sales

2 Harry C C Legal

3 John B A Marketing

3 John B B Sales

3 John B C Legal
EXAMPLE 3 :
Summary

● Cartesian Product in DBMS is an operation used to merge columns


from two relations.
● Cartesian or Cross Product is a binary set operation means, at a time we
can apply the operation on two relations.
● The Cartesian Product between two relations R1 and R2 consists of all
the possible combinations of R1 rows and R2 rows.
● The number of rows will be equal to the number of rows of R1
multiplied by the number of rows of R2.
REFERENCES :

1. Academic Webpages :

● https://www.guru99.com/relational-algebra-dbms.html
● https://www.sciencedirect.com/topics/computer-science
● https://www.tutorialspoint.com/sql/sql-cartesian-joins.htm#:~:text=The
%20CARTESIAN%20JOIN%20or%20CROSS,is%20absent%20from%20the
%20statement.
● https://www.geeksforgeeks.org/cartesian-product-operation-in-relational-algebra

1. Text Books
Thank You!

You might also like