Updated July 1, 2023
Introduction to SQL Cross Join
In SQL, Cross Join is a clause used to join records from two or more tables based on a related field common in both 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 with the row in the second table having a common column to produce the result set, which combines all rows from both 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.
The 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…
- In Maths, the Cartesian product is the product operation that provides 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 pair 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)}
- The Cross Join matches every row from the first table with every row in the second table, combining all rows and columns from both tables in the result set.
How does SQL Cross Join work?
- People commonly use this technique to combine each row from multiple tables. The Cross Product Join is the name of the table that displays all possible combinations of rows.
- Good 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 a 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 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 gets paired to the 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 a SELECT statement and adding the table names after a From clause.
Examples to Implement SQL Cross Join
Let us consider some practical examples to explain the SQL Cross Join further. 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 a 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 in a database include information about their customers and the respective order details tables. We have inserted some demo records to apply the Join using the SQL statement 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');
We can combine these tables using SQL query statements to obtain a result set similar to a Cartesian join:
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 the result table with all the rows of one table and others forming a Cartesian product joining.
3. SELECT
Both tables have three rows of data, 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. Per the query, the result set returns 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, 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.