Combining data from several tables is an ordinary procedure in the field of relational databases. The Cross Join is one of the several table joining techniques offered by PL/SQL, Oracle's procedural SQL extension.
To improve your comprehension, this article will provide you with an introduction to Cross Joins in PL/SQL along with a synopsis useful examples and visual aids.
PL/SQL Cross Join
One of the techniques provided by PL/SQL (Oracle's procedural SQL extension) is the cross-join. A Cross Join is also known as a Cartesian Join and returns the Cartesian product of two tables.
It combines every row from the first table with every row from the second table. Unlike conventional joins Cross Joins do not require any matching condition between the tables.
Syntax:
SELECT column1, column2, ...
FROM table1
CROSS JOIN table2;
Examples of PL/SQL Cross Join
This example demonstrates how to create a table named Employees
in PL/SQL and insert data into it.
The Employees
table contains two columns: EmployeeID
(the primary key) and Name
. After inserting two rows, the table stores the names "John" and "Jane" with their respective IDs.
1. Employees Table
The query creates an Employees
table with EmployeeID
as the primary key and inserts two employees, "John" and "Jane". This sets up a basic structure to store employee information, which can later be used in further operations like joins.
Query:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR2(50)
);
INSERT INTO Employees (EmployeeID, Name) VALUES (1, 'John');
INSERT INTO Employees (EmployeeID, Name) VALUES (2, 'Jane');
Output:
Explanation:
The table Employees
now contains two records with employee IDs 1 and 2, corresponding to the names "John" and "Jane." These records will later be used in a Cross Join to combine with other tables.
Department Table
The query first creates a table named Departments
with two columns:
DepartmentID
: This is an integer that acts as the primary key, ensuring that each department has a unique identifier.
DepartmentName
: This column stores the name of the department as a string with a maximum length of 50 characters.
Query:
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR2(50)
);
INSERT INTO Departments (DepartmentID, DepartmentName) VALUES (1, 'HR');
INSERT INTO Departments (DepartmentID, DepartmentName) VALUES (2, 'IT');
Output:
DepartmentID | DepartmentName |
---|
1 | HR |
2 | IT |
Explanation:
- The output shows the contents of the
Departments
table after the INSERT
statements.
- The first row has a
DepartmentID
of 1, representing the "HR" department.
- The second row has a
DepartmentID
of 2, representing the "IT" department.
These entries will be useful in future cross joins, where you can combine employee information with departments to form a Cartesian product.
Example 1: Basic Cross Join
The query performs a CROSS JOIN between the Employees
and Departments
tables, retrieving all possible combinations of employee names and department names. This operation produces the Cartesian product, meaning every row in the Employees
table is paired with every row in the Departments
table.
Query:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
CROSS JOIN Departments;
Output:
Name | DepartmentName |
---|
John | HR |
John | IT |
Jane | HR |
Jane | IT |
Explanation:
The output shows all possible combinations of employees and departments. Each employee (John, Jane) is paired with both departments (HR, IT), producing four rows. This demonstrates the nature of a cross join, where no condition is required to match rows from the two tables.
Example 2: Cross Join with a Condition
This example demonstrates how a Cross Join is used to generate all possible combinations between two tables, such as products and colors. By using a condition, you can further refine or filter the combinations generated by the Cross Join, helping to analyze data more effectively in practical scenarios.
Products Table
The query creates a Products table with two columns: ProductID
and ProductName
, and inserts two rows: one for a laptop and one for a phone. This sets up the table for further operations such as joins or other queries.
Query:
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR2(50)
);
INSERT INTO Products (ProductID, ProductName) VALUES (1, 'Laptop');
INSERT INTO Products (ProductID, ProductName) VALUES (2, 'Phone');
Output:
ProductID | ProductName |
---|
1
| Laptop
|
2
| Phone
|
Explanation:
The output shows the Products
table with two entries. Each row represents a product, and this data can be used in future cross joins to pair products with other data, such as colors or categories.
Colors Table
The provided SQL query accomplishes two key tasks. First, it creates a table named Colors
with two columns: ColorID
that serves as the primary key ensuring each value is unique and ColorName,
a variable character field with a maximum length of 50 characters for storing color names.
Next, it inserts two rows into the Colors
table: the first row with ColorID
1 and ColorName
'Red', and the second row with ColorID
2 and ColorName
'Blue'.
Query:
CREATE TABLE Colors (
ColorID INT PRIMARY KEY,
ColorName VARCHAR2(50)
);
INSERT INTO Colors (ColorID, ColorName) VALUES (1, 'Red');
INSERT INTO Colors (ColorID, ColorName) VALUES (2, 'Blue');
Output:
ColorID | ColorName |
---|
1 | Red |
2 | Blue |
Explanation:
The output confirms the successful creation of the table and insertion of these values, showing a list of color IDs and their corresponding names.
Cross Join with Product-Color Combinations
This query demonstrates the use of a CROSS JOIN
to generate all possible combinations of products and colors. The CROSS JOIN
operation combines each row from the Products
table with each row from the Colors
table, resulting in a Cartesian product of the two tables.
Query:
SELECT Products.ProductName, Colors.ColorName
FROM Products
CROSS JOIN Colors;
Output:
ProductName | ColorName |
---|
Laptop | Red |
Laptop | Blue |
Phone | Red |
Phone | Blue |
Explanation:
The output lists every possible pairing of products with colors. Specifically, it shows:
- 'Laptop' paired with 'Red' and 'Blue'
- 'Phone' paired with 'Red' and 'Blue'
This result is expected because the CROSS JOIN
does not require any conditions and generates a combination for every row in Products
with every row in Colors
, resulting in a comprehensive list of product-color combinations.
Important Points About Cross Joins
- CROSS JOIN returns the Cartesian product of records from two or more joined tables.
- It is used when we want to obtain every possible combination of rows from the tables.
- When used with a WHERE clause, CROSS JOIN behaves like an INNER JOIN, filtering results based on the given condition.
- It differs from other join types (like INNER, LEFT, or RIGHT JOIN) as it does not require any matching condition between the tables.
Conclusion
Cross joins in PL/SQL are a powerful tool for generating Cartesian products of tables, providing all possible combinations of rows between two or more tables. They are especially useful in scenarios where you need to explore or analyze every pairing of data.
While cross joins do not require matching conditions like other joins, they can create large result sets, so they should be used judiciously. By understanding how to set up and interpret cross joins, you can use this technique to gain valuable insights from your database.
Similar Reads
SQL CROSS JOIN
In SQL, the CROSS JOIN is a unique join operation that returns the Cartesian product of two or more tables. This means it matches each row from the left table with every row from the right table, resulting in a combination of all possible pairs of records. In this article, we will learn the CROSS JO
3 min read
MySQL CROSS JOIN
MySQL is an open-source relational database management system that uses Structured Query Language (SQL) to manipulate databases. It stores data in a table format. It provides various statements to perform Create, Read, Update, and Delete operations on a database table. Among these operations, MySQL
5 min read
PL/SQL JOIN
JOIN is a powerful operation in PL/SQL that allows us to combine data from two or more related tables based on a common key. The PL/SQL JOIN is used to select data from multiple tables using this key to match records. This powerful PL/SQL feature allows for selecting data across multiple tables usin
5 min read
PL/SQL Full Join
A FULL JOIN, also called a FULL OUTER JOIN, is a type of join in PL/SQL that returns all rows from both tables, even if there is no matching data in the other table. If a row from one table doesnât have a match in the other, it will still be included in the result, with NULL values filling in the mi
6 min read
PL/SQL DELETE JOIN
In database management, removing specific records from a table is a common task especially when cleaning or updating data. One useful technique is the DELETE JOIN which allows us to delete records from one table based on conditions involving another table.In this article, we will explore PL/SQL DELE
4 min read
PL/SQL Left Join
In the world of database management, efficiently retrieving and combining data from multiple tables is crucial. Among these the LEFT JOIN is particularly important because it includes all records from one table even when there are no corresponding records in another table. In this article, we will P
6 min read
SQL LEFT JOIN
In SQL, LEFT JOIN retrieves all records from the left table and only the matching records from the right table. When there is no matching record found, NULL values are returned for columns from the right table. This makes LEFT JOIN extremely useful for queries where you need to retain all records fr
5 min read
SQL Self Join
A Self Join in SQL is a powerful technique that allows one to join a table with itself. This operation is helpful when you need to compare rows within the same table based on specific conditions. A Self Join is often used in scenarios where there is hierarchical or relational data within the same ta
4 min read
CROSS APPLY vs INNER JOIN in PL/SQL
PL/SQL Stands for procedural language extension to SQL. In a procedure, the role of the subprogram is to perform a particular task and it is a unit module of a program. It combined to form larger programs. A subprogram can be involved by another program which is called the calling program. PL/SQL pr
6 min read
PL/SQL Right Join
In the area of database management, efficiently retrieving and combining data from multiple tables is essential. Among these techniques, the RIGHT JOIN is particularly useful because it includes all records from one table, even when there are no corresponding records in another table.PL/SQL Right Jo
4 min read