SQL Tutorial

Understanding SQL CROSS JOIN with Examples

SQL CROSS JOIN
SQL CROSS JOIN

SQL CROSS JOIN

SQL JOIN operations are used to combine data from multiple tables into a single result set. Among the different types of joins, CROSS JOIN, also called a Cartesian Join, is one of the simplest because it combines every row from the first table with every row from the second table.

The total number of rows produced by a CROSS JOIN is calculated by multiplying the number of rows in both tables. For example, if Table 1 contains 2 rows and Table 2 contains 3 rows, the CROSS JOIN produces 2 × 3 = 6 rows.

Understanding SQL CROSS JOIN with Examples

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

SQL Syntax for CROSS JOIN

SELECT TableName1.columnName1, TableName2.columnName2
FROM TableName1
CROSS JOIN TableName2;

This syntax generates all possible combinations of rows between the two specified tables.

Let’s understand the CROSS JOIN operation in more detail with the following MySQL examples.

Example 1: CROSS JOIN Between MatchScore and Departments Tables

Given Tables:

MatchScore

PlayerDepartment_idGoals
Franklin12
Alan13
Priyanka22
Rajesh35

Departments

Department_idDepartment_name
1IT
2HR
3Marketing

Query:

SELECT * FROM MatchScore
CROSS JOIN Departments;

Output:

The MatchScore table contains 4 rows, while Departments contains 3 rows. Therefore, the CROSS JOIN produces 4 × 3 = 12 rows.

PlayerDepartment_idGoalsDepartment_idDepartment_name
Franklin121IT
Alan131IT
Priyanka221IT
Rajesh351IT
Franklin122HR
……………

As shown above, every record from MatchScore is paired with every record from Departments.

Example 2: CROSS JOIN Between Employee and Department Tables

Given Tables:

Employee

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

Department

DepartmentIDDepartment_Name
1Production
2Sales
3Marketing
4Accounts

Query:

SELECT * FROM Employee
CROSS JOIN Department;

Output:

There are 5 employees and 4 departments. The CROSS JOIN therefore creates 5 × 4 = 20 rows, pairing every employee with every department.

Example 3: CROSS JOIN Between Loan and Borrower Tables

Given Tables:

Loan

LoanIDBranchAmount
1B115000
2B210000
3B320000
4B4100000

Borrower

CustIDCustName
1Sonakshi Dixit
2Shital Garg
3Swara Joshi
4Isha Deshmukh

Query:

SELECT * FROM Loan
CROSS JOIN Borrower;

Output:

The Loan table contains 4 records and the Borrower table also contains 4 records. Consequently, the CROSS JOIN produces 4 × 4 = 16 rows.

Example 4: CROSS JOIN Between Customer and Orders Tables

Given Tables:

Customer

Customer_IDNameAgeSalary
1Aryan Jain5156000
2Arohi Dixit2125000
3Vineet Garg2431000

Orders

Order_IDOrder_DateAmount
12012-01-203000
22012-05-182000
32012-06-284000

Query:

SELECT * FROM Customer
CROSS JOIN Orders;

Output:

Because there are 3 customers and 3 orders, the resulting CROSS JOIN contains 3 × 3 = 9 rows.

Download New Real Time Projects :- Click here

Conclusion

The SQL CROSS JOIN combines every row from one table with every row from another table, producing a Cartesian product. The total number of rows in the result is determined by multiplying the number of rows in the two source tables.

Although CROSS JOIN can be useful in specific situations, it should be used carefully because combining large tables can produce a very large result set. Understanding how the Cartesian product works allows you to use CROSS JOIN effectively while keeping potential performance concerns in mind.


SEO Keywords

sql cross join vs full join
sql scross join example
sql cross join in sql example
self join in sql
sql cross join vs inner join
sql cross join syntax
sql cross join in oracle
sql cross join in mysql
sql joins
cross join
sql cross join example
sql cross join w3schools

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us