Updated March 23, 2023
Introduction to Oracle Cross Join
A Cross Join is also called as Cartesian Product, as it is a type of Join function that returns a result set by joining every row item of one table with each and every item of the other table. If table A is cross joined with another table B, where table A has 10 records and table B has 10 records, the result set will have 100 records. Cross Join in Oracle can have a WHERE condition optionally.
Syntax:
SELECT *
FROM table1
CROSS JOIN table2
[where condition];
Parameters:
- table1: Here we provide the name of the first table
- table2: Here we provide the name of the second table
- [where condition]: It is optional and it means that whatever records that satisfy the condition will be extracted.
As we can see the syntax is very simple and easy to understand. Next in this article, we are going to discuss the working of cross join.
How Cross Join Works in Oracle?
A cross join is a cross product of two tables or it is also called a Cartesian product. We need to understand the term Cartesian in this respect. So, a Cartesian product means that every row of one table is joined to all the rows of the other table. This results in a Cartesian product, suppose we have two tables with three and four rows respectively. So if we do cross join between those two tables we will have 4*3 =12 rows. So to generalize if we have two tables with x rows and y rows then the Cartesian product will be x*y and the same is for cross join and the number of rows that will be extracted from the two tables is equal to the Cartesian product between the two tables which is x*y in this case. One important point to note that things may change if we have where condition in the query because then the actual number of rows returned will also depend on whether where the condition is satisfied or not.
This explains to us the working of the cross join in oracle. Now we will look into various examples to understand better.
Examples to Implement Cross Join in Oracle
So now we are going to look at different examples to better understand oracle cross join. So, we will be working with two tables’ employee tables with six columns 12 rows and a vehicle table with four columns 9 rows.
Example #1 – Cross Join without Condition
In this example, we are going to try to find the simple Cartesian product or cross join between two tables stored in the database. The tables are employee and vehicle. As discussed the employee table has 12 rows and the vehicle table has 9 rows. So when we discussed earlier the working of cross join, we discussed that there is no join condition provided in this type of join which we get when we do outer or inner joins. The query for this can be written in two ways both of which provide us with the same output. Below both, the queries are written
Query:
SELECT * FROM employee, vehicle;
SELECT * FROM employee CROSS JOIN vehicle;
Both queries represent the same data. So, in this case, we are selecting all the records which we are extracting from the database. AS we have already discussed that cross join is a Cartesian product of two tables. The employee table has 12 rows and 6 columns whereas the vehicle table has 9 rows and 4 columns. As per Cartesian rule, it should be 12*9 =108 rows and 10 columns because every row of the employee will join with every other row of the vehicle.
The queries when running on Oracle SQL developer displays the below output.
Output:
If you see the above screenshot it shows 108 rows as well as all columns from the two tables.
Example #2 – Cross Join with Specific Columns but No Conditions
In this example it is similar to the earlier example we saw the only difference is that here only specific columns between two tables are to be extracted the working operation of the query remains the same as the earlier query. The query for the same is written below:
Query:
SELECT employee.employee_id, employee.name,
vehicle.vehicle_id, employee.VEHICLE_NAME
FROM employee CROSS JOIN vehicle;
If you see the query we have taken two columns from each table. Now let us execute the query in sql developer and the output screenshot is below.
As we can see the number of rows is the Cartesian product of the two tables explained earlier. Only the number of columns is four as we had mentioned the same in our query.
Example #3 – Cross Join with WHERE Condition
So, in this example, we will use the WHERE condition in our query. When we apply the WHERE clause with the condition the records which after the Cartesian product of the two tables satisfy the condition of the WHERE clause will be extracted. We will be using the same two tables used for earlier examples. The query for the operation is provided below.
Query:
SELECT employee.employee_id, employee.name,
vehicle.vehicle_id, vehicle.VEHICLE_NAME
FROM employee CROSS JOIN vehicle
WHERE vehicle.VEHICLE_NAME='Honda';
Now if we see at the end of the query it has a condition which states that only those records which have value as ‘Honda’ in the vehicle_name column of the vehicle table will be extracted as output.
Let us run the above query in the Oracle SQL database and the output of the same is shown below as screenshot.
Output:
If we see the output closely we can see that we only have ‘Honda’ as values in the vehicle_name column for all records. The number of rows count is also 24 which mean that there were only twenty-four records who has vehicle name as ‘Honda’.
Recommended Articles
This is a guide to the Oracle Cross Join. Here we discuss how to implement Cross Join using different conditions in Oracle along with examples. You may also look at the following articles to learn more –