The Wayback Machine - https://web.archive.org/web/20241011121558/https://www.geeksforgeeks.org/pl-sql-join/
Open In App

PL/SQL JOIN

Last Updated : 09 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 using the common key to match records. In this article, we will explain various types of PL/SQL JOINs, including examples and syntax .

PL/SQL JOIN

There are different types of joins in PL/SQL, and they help in retrieving specific results for complex queries. The JOIN statement in PL/SQL enables the user to group records by joining two or more tables based on dependency. There are different types of joins we can apply based on the requirement of the query.

Syntax:

SELECT columns
FROM table1
[INNER | LEFT | RIGHT | FULL] JOIN table2
ON table1.common_column = table2.common_column;

Key terms:

  • columns: Specify the columns we want to retrieve.
  • table1, table2: Specify the names of the table being joined.
  • common_column: The column that is used to establish the relationship between the tables.

Types of PL/SQL JOIN

  • INNER JOIN: Records are returned only when there is a match between the key columns of both tables. It is commonly used to fetch related data from multiple tables where the values in the specified columns match
  • LEFT JOIN (LEFT OUTER JOIN): Brings all records from the left table and the matched records of the right table. Some rows will only have NULL values because they are not matched with any group.
  • RIGHT JOIN (RIGHT OUTER JOIN): They pull the records from the right table as well as the records matched to these from the left table. As we can see, unmatched rows will be marked with NULL.
  • FULL JOIN (FULL OUTER JOIN): Returns all records if they are matched in either left or right table. In case of unmatched rows, it will be NULL on the right side of the merge.

Creating Sample Tables and Inserting Data

To illustrate how to use statistical functions in PL/SQL effectively, we first need to set up our sample database structure. We create two tables Employees and Departments. After creating the tables, we insert sample data to facilitate statistical analysis and demonstrate the functionality of various PL/SQL statistical functions.

1. Employees Tables

CREATE TABLE Employees (
emp_id NUMBER PRIMARY KEY,
emp_name VARCHAR2(50),
dept_id NUMBER
);

INSERT INTO Employees (emp_id, emp_name, dept_id) VALUES (1, 'John', 101);
INSERT INTO Employees (emp_id, emp_name, dept_id) VALUES (2, 'Sarah', 102);
INSERT INTO Employees (emp_id, emp_name, dept_id) VALUES (3, 'Michael', 101);
INSERT INTO Employees (emp_id, emp_name, dept_id) VALUES (4, 'Emma', NULL);

Output:

emp_id emp_name dept_id
1 John 101
2 Sarah 102
3 Michael 101
4 Emma NULL

Departments Table

CREATE TABLE Departments (
dept_id NUMBER PRIMARY KEY,
dept_name VARCHAR2(50)
);

INSERT INTO Departments (dept_id, dept_name) VALUES (101, 'HR');
INSERT INTO Departments (dept_id, dept_name) VALUES (102, 'IT');
INSERT INTO Departments (dept_id, dept_name) VALUES (103, 'Marketing');

Output:

dept_id dept_name
101 HR
102 IT
103 Marketing

Examples of PL/SQL JOIN

Let’s demonstrate different types of joins with examples. We will use two tables for our examples: Employees and Departments.

Example 1: PL/SQL INNER JOIN

In PL/SQL INNER JOIN, records are returned only when there is a match between the key columns of both tables. It is commonly used to fetch related data from multiple tables where the values in the specified columns match. If there is no match, those rows are excluded from the result.

Query:

SELECT e.emp_name, d.dept_name
FROM Employees e
INNER JOIN Departments d
ON e.dept_id = d.dept_id;

Output:

A1
Inner Join

Explanation:

The query returns employees whose dept_id exists in both the Employees and Departments tables. Since Emma does not have a dept_id, she is excluded from the result.

Example 2: PL/SQL LEFT JOIN

A LEFT JOIN returns all records from the left table Employees and the matched records from the right table Departments. If there is no match, NULL values will be returned for columns from the right table.

Query:

SELECT e.emp_name, d.dept_name
FROM Employees e
LEFT JOIN Departments d
ON e.dept_id = d.dept_id;

Output:

A2
Left Join

Explanation:

This query shows all employees, including Emma, who has no department assigned. In her case, the dept_name column returns NULL because there is no matching record in the Departments table.

Example 3: RIGHT JOIN

A RIGHT JOIN returns all records from the right table Departments and the matched records from the left table Employees. If there is no match, NULL values are returned for columns from the left table.

Query:

SELECT e.emp_name, d.dept_name
FROM Employees e
RIGHT JOIN Departments d
ON e.dept_id = d.dept_id;

Output:

A3
Right Join

Explanation:

The result shows all departments, even if there’s no employee in them. The Marketing department has no employees, so emp_name is NULL for that record..

Example 4: FULL JOIN

A FULL JOIN returns all records when there is a match in either table. Unmatched rows will show NULL for the missing values.

Query:

SELECT e.emp_name, d.dept_name
FROM Employees e
FULL JOIN Departments d
ON e.dept_id = d.dept_id;

Output:

A4
Full Join

Explanation:

This query returns all employees and departments, including unmatched records like Emma (without a department) and the Marketing department (with no employees).

Conclusion

PL/SQL JOIN is an important concept that allows to combine data from multiple tables based on a common column, also known as a key. It offers different types of joins, such as INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN. Understanding these types of joins helps to efficiently retrieve related data from multiple tables.



Previous Article
Next Article

Similar Reads

Full join and Inner join in MS SQL Server
Full Join Full join selects all the rows from the left and right tables along with the matching rows as well. If there are no matching rows, it will be displayed as NULL. Syntax: select select_list from table1 full join table2 on join _predicate (OR) select table1.*, table2.* from table1 full join table2 on join _predicate (OR) select * from table1
2 min read
Difference between Inner Join and Outer Join in SQL
JOINS in SQL are fundamental operations used to combine data from multiple tables based on related columns. They are essential for querying data that is distributed across different tables, allowing you to retrieve and present it as a single or similar result set. In this article, We will learn about Inner Join vs Outer Join in SQL in detail. What
5 min read
Difference Between “INNER JOIN” and “OUTER JOIN”
Joins in SQL are essential tools that allow us to combine rows from multiple tables based on specific conditions, often involving a relation between columns in those tables. These joins help in pulling related data together in a meaningful way. Among the most commonly used joins are INNER JOIN and OUTER JOIN. In this article, We will learn about th
5 min read
Difference Between Anti-Join and Semi-Join
In the context of SQL, Anti-join, and semi-join are two essential operations in relational databases used for querying and manipulating data. These operations focus on comparing data from two related tables, but they serve distinct purposes. In this article let us discuss these two operations in detail along with some examples. What is SQL Join?Joi
5 min read
Difference Between Left Join and Left Outer Join
In SQL language, different joins are used to assemble rows from two or more tables from the related column. The terms "Left Join" and "Left Outer Join" are used interchangeably in SQL but they refer to the same concept. A Left Join retrieves all records from the left table (the first table in the query) and matches them with records from the right
5 min read
Difference Between Right Join and Right Outer Join
Joins in a Database (SQL) are mostly used for combining data or the rows of two or more table records that are based on the same or common attribute. There are various types of Joins like Right Join, Left Join, Full Join, etc. Each join has its own syntax and data-returning capability. In this article, we will see the information about Right Join a
5 min read
SQL Server FULL OUTER JOIN
Joins in SQL are used to retrieve data from multiple tables based on a related column (or common column) between them. In this article, we will learn how to use FULL OUTER JOIN, which returns all rows from both tables being joined. It combines the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN. Instead of discarding a row with no match, it fi
6 min read
MySQL UPDATE JOIN
A widely used open-source relational database management system that allows you to efficiently store, organize, and retrieve data. Developed by Oracle, My SQL is widely used for building and managing databases that handle interactive websites and applications. We'll discuss the syntax, and demonstrate how this dynamic duo can efficiently modify dat
6 min read
SQL Server UPDATE JOIN
In the expansive realm of SQL Server, the UPDATE JOIN operation emerges as a potent tool for modifying data within tables by combining information from multiple sources. Unlike a traditional UPDATE statement that modifies a single table, UPDATE JOIN enables the modification of records based on conditions involving multiple tables. This capability p
6 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 provides a block structure of executable unit code.
6 min read
How to Join First Row in SQL?
SQL(Structured Query Language) is a powerful tool that is used to manage and manipulate data in a database. Join is a very important tool in SQL that combines multiple tables based on related columns between them. Joining the first row of tables in SQL is a very useful technique when you need to fetch additional information from the tables. In this
5 min read
How Inner Join works in LINQ to SQL
LINQ (Language Integrated Query) in C# provides a powerful way to query data from various data sources, including databases. In LINQ to SQL, which is specifically designed for working with relational databases, an inner join is a common operation used to combine records from two tables based on a specified condition. This article explores the conce
3 min read
FULL OUTER JOIN in SQLite
In the area of data querying and manipulation, the ability to combine information from different sources is important. SQLite, a popular embedded database engine, offers a range of join operations to fast process. One such operation FULL OUTER JOIN is particularly powerful as it allows us to merge data from two tables, even if there are no direct m
4 min read
How to Join to First Row in SQL Server
Joining the first row in SQL Server can be a common requirement in various scenarios, such as when we need to retrieve specific data associated with the first occurrence of a particular group or when you want to fetch additional details related to the first record in a result set. In this article, We will learn about How to Join to first row in SQL
4 min read
When should we use CROSS APPLY over INNER JOIN in MySQL
CROSS APPLY and INNER JOIN are used in MySQL to combine data from multiple tables. When dealing with row-wise operations or dynamic subqueries, CROSS APPLY is more efficient than INNER JOIN. CROSS APPLY allows for more detailed control on individual rows, while INNER JOIN operates on sets of data. It becomes very useful when we need to perform row-
4 min read
How to Perform the SQL Join Equivalent in MongoDB?
In database management, with the rise of NoSQL databases like MongoDB, the approach to handling data relationships has evolved. MongoDB's document-oriented nature and schema-less design provide a different perspective on joins compared to SQL. In this article, we'll learn about How to Perform the SQL Join Equivalent in MongoDB by understanding thei
6 min read
How to Use Full Outer Join in MySQL
The FULL OUTER JOIN or FULL JOIN in MySQL is a powerful technique to fetch all rows from both tables, including matched and unmatched records. In this article, we will explore how to use FULL OUTER JOIN with practical examples and MySQL queries. Although MySQL doesn't natively support FULL OUTER JOIN, we can achieve the same result using a combinat
4 min read
MySQL SELF JOIN
Joins are very important for effective data retrieval and analysis. The 'JOIN' clause is used to combine data from two or more tables using the common column between them. In MySql, there are many types of joins like INNER JOIN, OUTER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, and SELF JOIN. In this article, we'll explore the concept of MySQL SELF JOI
5 min read
5 Best Practices For Writing SQL JOIN Query
The SQL JOIN clause allows users to take records from two or more tables at once and retrieve data based on a common field. It also helps to keep our database normalized. However, poorly written joins can lead to inefficient queries, slow performance, and even incorrect results. Here, we will explore the 5 best practices for writing SQL joins, and
5 min read
MySQL LEFT JOIN
In databases, data is often stored in multiple tables, making it necessary to combine them to fetch required information. MySQL JOIN statements enable merging tables based on common columns. In this article, we'll explore the MySQL LEFT JOIN keyword, a type of outer join that returns all records from the left table and matched records from the righ
5 min read
MySQL DELETE JOIN
MySQL is an open-source, user-friendly, powerful, and popular choice, relational database management system. When maintaining and modifying data, tables usually interact in a complex way. MySQL's DELETE JOIN function is one of its most powerful functions. MySQL DELETE JOIN is explored in detail in this article, which also offers examples to help vi
4 min read
MySQL Inner Join
In MySQL, the INNER JOIN clause is used to combine rows from two or more tables based on a related column between them. The INNER JOIN returns rows when there is at least one match in both tables. If there are rows in the left table that do not have matches in the right table, those rows will not be included in the result set. This is one of the mo
7 min read
MySQL RIGHT JOIN
In databases, data is stored in multiple tables and it is often necessary sometimes to combine two or more tables to fetch the required data. In MySQL, joins enable the merging of multiple tables based on the common columns. In this article, we are going to explore MySQL RIGHT JOINS which is a type of outer join in MySQL. MySQL RIGHT JOINIn MySQL,
5 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 also provides the CROSS JOIN statement to combine
5 min read
INNER JOIN ON vs WHERE clause in MySQL
When working with MySQL queries that involve multiple tables, understanding how to effectively use INNER JOIN ON versus the WHERE clause can significantly impact query performance and clarity. These two SQL constructs serve distinct purposes in combining data from different tables based on specific conditions. In this article, we will explore the d
5 min read
How to Select the Last Records in a One-To-Many Relationship Using SQL Join
SQL (Structured Query Language) is a powerful tool used to communicate with and manage databases. Think of a database as a digital filing cabinet where you store information. Imagine a database as a digital filing cabinet where information is stored. SQL helps you interact with this filing cabinet, allowing you to perform tasks like adding, updatin
9 min read
How to Join two Schemas in Mongoose?
Mongoose is a popular MongoDB object modeling tool for Node.js that simplifies data manipulation. However, developers often face challenges when trying to join two schemas in Mongoose, as MongoDB, a NoSQL database, does not natively support join operations like SQL databases. In this article, We will learn about How to join two schemas in Mongoose
5 min read
MySQL Outer Join
MySQL is a popular and strong system that is extensively employed for handling data in the field of relational databases. A crucial aspect of working with databases is the ability to retrieve and handle data from different tables. SQL joins combine rows from different tables by connecting them based on related columns. The Outer Join is an importan
4 min read
Conditional Join
DBMS or Database Management Systems consist of data collected from various sources. Database administrators and analysts use this data to analyze the collected data. Database administrators execute the query through which some output is generated, the conditions are passed through the query. This query can vary from simple to complex query. Joins i
5 min read
MySQL JOIN
MySQL JOIN is a fundamental feature that combines rows from two or more tables based on a related column between them. It allows for efficient data retrieval by enabling the extraction of related information from multiple tables in a single query. In this article, We will learn about MySQL JOIN by understanding various types of JOINS with the help
6 min read
Article Tags :