SQL Cross Join

  • Join operation in SQL is used to combine multiple tables together into a single table.
  • If we use the cross join to combine two different tables, then we will get the Cartesian product of the sets of rows from the joined table. When each row of the first table is combined with each row from the second table, it is known as Cartesian join or cross join.
  • After performing the cross join operation, the total number of rows present in the final table will be equal to the product of the number of rows present in table 1 and the number of rows present in table 2.
  • For example:
    If there are two records in table 1 and three records in table 2, then after performing cross join operation, we will get six records in the final table.
  • Let us take a look at the syntax of writing a query to perform the cross join operation in SQL.

Now let us see take a deeper dive into the cross join in SQL with the help of examples. All the queries in the examples will be written using the MySQL database.

Consider we have the following tables with the given data:

Table 1: MatchScore

PlayerDepartment_idGoals
Franklin12
Alan13
Priyanka22
Rajesh35

Table 2: Departments

Department_idDepartment_name
1IT
2HR
3Marketing

Table 3: employee

EmployeeIDEmployee_NameEmployee_Salary
1Arun Tiwari50000
2Sachin Rathi64000
3Harshal Pathak48000
4Arjun Kuwar46000
5Sarthak Gada62000

Table 4: department

DepartmentIDDepartment_NameEmployee_ID
1Production1
2Sales3
3Marketing4
4Accounts5

Table 5: loan

LoanIDBranchAmount
1B115000
2B210000
3B320000
4B4100000

Table 6: borrower

CustIDCustNameLoanID
1Sonakshi Dixit1
2Shital Garg4
3Swara Joshi5
4Isha Deshmukh2

Table 7: customer

Customer_IDNameAgeSalary
1Aryan Jain5156000
2Arohi Dixit2125000
3Vineet Garg2431000

Table 8: orders

Order_IDOrder_DateCutomer_IDAmount
12012-01-2023000
22012-05-1822000
32012-06-2834000

Example 1:

Write a query to perform the cross join operation considering the MatchScore table as the left table and the Departments table as the right table.

Query:

We have used the SELECT command with the asterisk to retrieve all the columns present in the MatchScore and Departments table. Then we have used the CROSS JOIN keyword to perform the cross join operation on the MatchScore and Departments table. Since there are 4 records in the MatchScore and 3 records in the Departments table, after performing the cross join operation, we will get 12 rows.

After executing this query, you will find the following result:

PlayerDepartment_idGoalsDepatment_idDepartment_name
Franklin121IT
Alan131IT
Priyanka221IT
Rajesh351IT
Franklin122HR
Alan132HR
Priyanka222HR
Rajesh352HR
Franklin123Marketing
Alan133Marketing
Priyanka223Marketing
Rajesh353Marketing

Each row from the MatchScore table is combined with each row of the Departments table. Since there are four records in the MatchScore and three records in the Departments table, we have got 12 rows in the final table after performing the cross join operation.

Example 2:

Write a query to perform the cross join operation considering the employee table as the left table and the department table as the right table.

Query:

We have used the SELECT command with the asterisk to retrieve all the columns present in the employee and department table. Then we have used the CROSS JOIN keyword to perform the cross join operation on the employee and department table. Since there are five records in the employee and four records in the department table, after performing the cross join operation, we will get 20 rows.

After executing this query, you will find the following result:

EmployeeIDEmployee_NameEmployee_SalaryDepartmentIDDepartment_NameEmployee_ID
1Arun Tiwari500001Production1
1Arun Tiwari500002Sales3
1Arun Tiwari500003Marketing4
1Arun Tiwari500004Accounts5
2Sachin Rathi640001Production1
2Sachin Rathi640002Sales3
2Sachin Rathi640003Marketing4
2Sachin Rathi640004Accounts5
3Harshal Pathak480001Production1
3Harshal Pathak480002Sales3
3Harshal Pathak480003Marketing4
3Harshal Pathak480004Accounts5
4Arjun Kuwar460001Production1
4Arjun Kuwar460002Sales3
4Arjun Kuwar460003Marketing4
4Arjun Kuwar460004Accounts5
5Sarthak Gada620001Production1
5Sarthak Gada620002Sales3
5Sarthak Gada620003Marketing4
5Sarthak Gada620004Accounts5

Each row from the employee table is combined with each row of the department table. Since there are five records in the employee table and four records in the department table, we have got 20 rows in the final table after performing the cross join operation.

Example 3:

Write a query to perform the cross join operation considering the loan table as the left table and the borrower table as the right table.

Query:

We have used the SELECT command with the asterisk to retrieve all the columns present in the loan and the borrower table. Then we have used the CROSS JOIN keyword to perform the cross join operation on the loan and the borrower table. Since there are four records in the loan table and four records in the borrower table, after performing the cross join operation, we will get 16 rows.

After executing this query, you will find the following result:

LoanIDBranchAmountCustIDCustNameLoanID
1B1150001Sonakshi Dixit1
2B2100001Sonakshi Dixit1
3B3200001Sonakshi Dixit1
4B41000001Sonakshi Dixit1
1B1150002Shital Garg4
2B2100002Shital Garg4
3B3200002Shital Garg4
4B41000002Shital Garg4
1B1150003Swara Joshi5
2B2100003Swara Joshi5
3B3200003Swara Joshi5
4B41000003Swara Joshi5
1B1150004Isha Deshmukh2
2B2100004Isha Deshmukh2
3B3200004Isha Deshmukh2
4B41000004Isha Deshmukh2

Each row from the loan table is combined with each row of the borrower table. Since there are four records in the loan table and four records in the borrower table, after performing the cross join operation, we have got 16 rows.

Example 4:

Write a query to perform the cross join operation considering the customer table as the left table and the orders table as the right table.

Query:

We have used the SELECT command with the asterisk to retrieve all the columns present in the customer and orders table. Then we have used the CROSS JOIN keyword to perform the cross join operation on the customer table and the orders table. Since there are three records in the loan table and three records in the orders table, after performing the cross join operation, we will get 9 rows.

After executing this query, you will find the following result:

Customer_IDNameAgeSalaryOrder_IDOrder_DateCustomer_IDAmount
1Aryan Jain515600012012-01-2023000
2Arohi Dixit212500012012-01-2023000
3Vineet Garg243100012012-01-2023000
1Aryan Jain515600022012-05-1822000
2Arohi Dixit212500022012-05-1822000
3Vineet Garg243100022012-05-1822000
1Aryan Jain515600032012-06-2834000
2Arohi Dixit212500032012-06-2834000
3Vineet Garg243100032012-06-2834000

Each row from the customers' table is combined with each row of the orders table. Since there are three records in the loan table and three records in the orders table, after performing the cross join operation, we will get 9 rows.


Next TopicSQL Primary Key




Latest Courses