SQL CROSS JOIN

Understanding SQL CROSS JOIN with Examples

Understanding SQL CROSS JOIN with Examples

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

SQL CROSS JOIN

The JOIN operation in SQL allows us to combine multiple tables into a single dataset. One of the simplest types of joins is the CROSS JOIN, also known as a Cartesian Join. This operation returns the Cartesian product of the sets of rows from the joined tables. This means that each row from the first table is combined with every row from the second table, leading to a final table whose total number of rows is equal to the product of the number of rows in each table.

For example, if Table 1 has 2 rows and Table 2 has 3 rows, performing a CROSS JOIN will result in 2 × 3 = 6 rows in the final dataset.

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

SQL Syntax for CROSS JOIN

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

This basic syntax retrieves all possible row combinations between the two tables.

Now, let’s explore CROSS JOIN in detail using 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:

After executing this query, the result will contain 4 × 3 = 12 rows:

PlayerDepartment_idGoalsDepartment_idDepartment_name
Franklin121IT
Alan131IT
Priyanka221IT
Rajesh351IT
Franklin122HR

Each row from MatchScore is combined with every row from Departments.

See also  SQL INSERT STATEMENT

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:

After execution, we get 5 × 4 = 20 rows combining each employee with each 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:

Since there are 4 loans and 4 borrowers, the resulting dataset contains 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:

Since there are 3 customers and 3 orders, we get 3 × 3 = 9 rows.

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

Conclusion

SQL’s CROSS JOIN function combines every row of one table with every row of another table to produce a Cartesian product.
The product of the number of rows in the two tables determines the total number of rows in the result set.
Although helpful in certain situations, CROSS JOIN should be used with caution because it can generate enormous databases.

See also  GROUP BY vs ORDER BY in SQL

You may take advantage of CROSS JOIN’s capabilities in SQL queries while keeping performance issues in mind if you understand it.


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
sql cross join multiple columns

🎓 Need Complete Final Year Project?

Get Source Code + Report + PPT + Viva Questions (Instant Access)

🛒 Visit UpdateGadh Store →
💬 Chat Now