What is Cartesian Product in SQL Server?

First create two tables Employee, Department.

Step 1: Create 'Employee' table here:

create table Employee(EmpId int primary key, EmpName varchar(30), DeptId int)

Step 2: Insert data into Employee table.

insert into Employee values(900,'AAA',501)
insert into Employee values(901,'BBB',502)
select * from Employee

Output:

EmpIdEmpNameDeptId
900AAA501
901BBB502

 

Step 3: Create 'Department' table here:

create table Department(DeptCode varchar(5) primary key, DeptName varchar(18))

Step 4: Inser data into Department table.

insert into Department values('CSE','This is CSE Department')
insert into Department values('ECE','This is ECE Department')
insert into Department values('EEE','This is EEE Department')
insert into Department values('MECH','This is Mechanical Department')
insert into Department values('IT','This is IT Department')
select * from Department

Output:

DeptCodeDeptDescription
CSEThis is CSE Department
ECEThis is ECE Department
EEEThis is EEE Department
ITThis is IT Department
MECHThis is Mechanical Department

 

Catresian Product:


Syntax:

select * from <Table1>, <Table2>

Example:

select * from Employee, Department

Output:

EmpIdEmpNameDeptIdDeptCodeDeptDescription
900AAA501CSEThis is CSE Department
900AAA501ECEThis is ECE Department
900AAA501EEEThis is EEE Department
900AAA501ITThis is IT Department
900AAA501MECHThis is Mechanical Department
901BBB502CSEThis is CSE Department
901BBB502ECEThis is ECE Department
901BBB502EEEThis is EEE Department
901BBB502ITThis is IT Department
901BBB502MECHThis is Mechanical Department

 

Note: 

If Table1 has 2 rows and Table2 has 5 rows then the Cartesian product of Table1 and Table2 is 10 rows. i.e., (Table1 rows * Table2 rows) = 10.