Introduction to SQL Cross Join
Let us study about SQL Cross Join. In SQL, Cross Join is a clause that is used to join records from two or more tables based on a related field common in both the tables. Normally, we can say that a SQL Cross Join operates a join process that returns the Cartesian product of two or more tables. In the Cartesian join, the rows in the first table are paired to the row in the second table having a common column to produce the result set which is the combination of all rows from both the table data. Thus, we can also compare this SQL Cross Join clause to the Inner Join clause where the join condition is always calculated to either true or where the join condition or statement is not included in the SQL query.
Syntax for Cross Join in SQL
We can consider the following basic syntax for this Cross Join clause in SQL:
SELECT table1.column1, table2.column2…tableN.columnN From table1,table2 [, tableN]
- Also, we can follow a simple syntax with the SELECT statement as follows:
Select * From table1 CROSS JOIN table2;
Or
Select * From table1,table2…
- As we know that in Maths, the Cartesian product is the product operation that provides us the result set of products from two separate tables by taking the multiply of rows from the first table to the second.
- For Example, we take two sets of items, firstly A={a,b,c} and secondly B={x,y,z}. Now let us find the Cartesian product between them, the result of A*B will be the ordered paired of these two tables A and B. So, the output is as follows where the sets A(a,b,c) & B(x,y,z) are multiplied to each other:
A*B ={(a,x),(a,y),(a,z),(b,x),(b,y),(b,z),(c,x),(c,y),(c,z)}
- Similarly, in Cross Join each row of the first table is paired to each row in the second table and thus forms a result set with combinations of all rows and columns of both tables.
How does SQL Cross Join work?
- It is normally used when we want to make combinations of each row from a couple of tables or more. This result table of all row pairs is known as Cross Product Join
- Useful use of this SQL Cross Join is to obtain all the combinations of items from two different records of two different collections of tables such as colors or sizes, etc. at the business level or where the huge database of such type is present in the industries.
- Hence we can say that we get a Cartesian product when a Cross Join does not use a WHERE clause where the rows in the first table are multiplied by the rows in the second table in a database.
- Suppose if we have two tables X & Y with ‘n’ and ‘m’ rows respectively and when we take the Cross join or Cartesian of these two tables, then each row of tables get paired to other and the result set contains n*m rows as the result rows of X*Y Cartesian product.
SELECT columnName From X CROSS JOIN Y;
- This is an alternative to achieve the same result set by using column names divided by commas after including SELECT statement and adding the table names after a From clause.
Examples to Implement SQL Cross Join
Let us consider some practical examples to further explain the SQL Cross Join in detail. Firstly, one more thing let me tell you that we can state a Cross Join in two different ways in SQL:
- By using the SQL Cross Join basic syntax with SELECT
- Using FROM clause without using a WHERE clause with SELECT
Thus, we can say that:
- SELECT * From TableName1 CROSS JOIN TableName2
- SELECT * From TableName1, TableName2
1. CREATE TABLE
For example, we have two tables ‘Customer_Data ’ and ‘Orders’. We have created the tables using the following SQL statements with fields Customer_Data(ID, Name, Address, Salary) and Orders(OID, CustomerID, Amount):
Code:
CREATE TABLE Customer_Data (ID int NOT NULL PRIMARY KEY, Name varchar(255) NOT NULL, Age int , Address varchar(255), Salary int );
Output:
Code:
CREATE TABLE Orders ( OID int NOT NULL PRIMARY KEY, CustomerID int, Amount int );
Output:
2. INSERT
The sample tables include some information about their customers and the respective order details tables in a database. We have inserted some demo records to apply the Join using the SQL statement as below:
Code:
INSERT INTO Customer_Data (ID, Name, Address, Salary)
VALUES ('1', 'Erica Smith', 'Norway','20000');
Code:
INSERT INTO Orders(OID,CustomerID,Amount)
VALUES ('01', '1', '2000');
Now we can join these tables to get the result set like Cartesian join using the SQL query Statements:
Code:
Select * From Customer_Data CROSS JOIN Orders;
Or
Select * From Customer_Data , Orders;
The above queries produce the same result.
Output:
Here, we have got the result table with all the rows of one table combined with others forming a Cartesian product joining.
3. SELECT
Both the tables have three rows of data and so their Cartesian product forms nine rows each paired with others. Also, we can select only some fields from the join clause to display as follows:
Code:
Select ID, Name,Amount From Customer_Data CROSS JOIN Orders;
Output:
So, we can view that we are getting the Cartesian product of each row in table Customer_Data with every row of table Orders. As per the query, the result set returns with the three fields, ID, Name, and Amount.
From the above explanation and SQL queries, we can consider that it allows us to form the Cartesian product of two or more tables from the joined table in SQL.
Conclusion
In a few databases like Oracle and PostgreSQL, you can consider using the SQL Inner Join clause to perform a SQL Cross Join as it always evaluates the true condition. Therefore hope in this article, you have learned how to implement the Cross Join clause in SQL to find out the Cartesian product of two or multiple tables in a database to access and combine the records with different fields including a Primary key.
Recommended Articles
We hope that this EDUCBA information on “SQL Cross Join” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
10 Online Courses | 8 Hands-on Projects | 80+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses