Open In App

PL/SQL Cross Join

Last Updated : 13 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

EmployeeID

Name

1

John

2

Jane

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

  1. CROSS JOIN returns the Cartesian product of records from two or more joined tables.
  2. It is used when we want to obtain every possible combination of rows from the tables.
  3. When used with a WHERE clause, CROSS JOIN behaves like an INNER JOIN, filtering results based on the given condition.
  4. 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.


Next Article
Article Tags :

Similar Reads