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 PL/SQL Left Join by understanding its syntax and various practical examples.
PL/SQL Left Join
A LEFT JOIN in PL/SQL combines data from two tables by returning all rows from the left table and the matched rows from the right table.
If there is no match in the right table then LEFT JOIN will still show all rows from the left table with NULLs in place of any missing data from the right table.
Key Points:
- Left Table: All the rows from the table will be included in result regardless of the whether there is matching rows in right table.
- Right Table: Only rows that have the corresponding matches in left table are to be included. If there is no match, NULL values are returned for the columns from right table.
- Use Case: LEFT JOIN is commonly used when we want to ensure that the all records from primary dataset (left table) will be included in the query result even if some related data (right table) is missing.
Syntax:
SELECT
columns
FROM
left_table
LEFT JOIN
right_table
ON
left_table.column_name = right_table.column_name;
Key terms:
- SELECT columns: It specifies which columns to retrieve, allowing us to select data from both the left and right tables.
- FROM left_table: Indicates the left table, from which all records will be included in the result.
- LEFT JOIN right_table: Specifies the right table and indicates that the join will include all records from the left table and matching records from the right table
- ON left_table.column_name = right_table.column_name: Defines the condition for matching rows between the tables, typically involving a foreign key in the left table and a primary key in the right table.
Examples of PL/SQL Left Join
In this example, we demonstrate how to create two tables, orders
and customers
, and use a LEFT JOIN
in PL/SQL to retrieve all records from the orders
table, along with matching records from the customers
table based on customer_id
.
Example 1:
Create a Orders table with order_id, customer_id and order_amount columns. The CREATE TABLE statement is used to create the orders table with the order_id, customer_id and order_amount attributes. The INSERT INTO statement is used to insert the values into the table with their respective columns.
Query:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_amount DECIMAL(10, 2)
);
INSERT INTO orders (order_id, customer_id, order_amount)
VALUES (1001, 201, 150.00);
INSERT INTO orders (order_id, customer_id, order_amount)
VALUES (1002, 202, 200.00);
INSERT INTO orders (order_id, customer_id, order_amount)
VALUES (1003, NULL, 50.00);
INSERT INTO orders (order_id, customer_id, order_amount)
VALUES (1004, 203, 300.00);
Output:
order_id | customer_id | order_amount |
---|
1001 | 201 | 150 |
---|
1002 | 202 | 200 |
---|
1003 | NULL | 50 |
---|
1004 | 203 | 300 |
---|
Explanation:
- The
order_id
is the primary key, and some orders might not have a corresponding customer, as seen in the case of customer_id
being NULL
.
- The table lists order details, including customer IDs associated with each order, except for one record (
order_id
1003), which has a NULL
customer ID.
Lets Create a customers table with customer_id and customer_name. In above code, the CREATE TABLE statement is used to create the customers table with customer_id and customer_name attributes.
INSERT INTO statement is used to insert the values into the table with their respective columns. This query is the same as the one used for the orders table, retrieving all records from orders and matching customer names from customers.
Query:
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR2(20)
);
INSERT INTO customers (customer_id, customer_name) VALUES (201, 'Michael');
INSERT INTO customers (customer_id, customer_name) VALUES (202, 'Sarah');
INSERT INTO customers (customer_id, customer_name) VALUES (203, 'David');
Output:
customer_id | customer_name |
---|
201 | Michael |
---|
202 | Sarah |
---|
203 | David |
---|
Explanation:
The above output table contains customer IDs and names for the customers who placed orders in the orders
table where customer_id
is the primary key.
Example 2: Filtering and Displaying Specific Columns
Given the `Orders` and `Customers` tables, we need to retrieve and display all orders along with the corresponding customer names. If an order does not have a matching customer, we still want to show the order details, with missing customer information represented as `NULL`.
Additionally, we want to filter the results to only show orders with an amount greater than 100.
SELECT o.order_id, o.order_amount, c.customer_name
FROM Orders o
LEFT JOIN Customers c
ON o.customer_id = c.customer_id
WHERE o.order_amount > 100;
Output:
order_id | order_amount | customer_name |
---|
1001 | 150.00 | Michael |
1002 | 200.00 | Sarah |
1004 | 300.00 | David |
Explanation:
In this query, we perform a LEFT JOIN between the Orders
and Customers
tables, similar to the first example. However, we add a WHERE
clause to filter the results to include only those orders with an order_amount
greater than 100.
- Orders with
order_amount
greater than 100 are included in the results. - The
customer_name
is included if there is a matching customer. - Orders with
order_amount
less than or equal to 100 (e.g., order_id
1003) are excluded from the result set.
Conclusion
In this article, we explored how to use SQL commands to manage and manipulate data across multiple tables in Oracle databases.
We focused on the LEFT JOIN operation in PL/SQL, which combines data from different tables while ensuring all records from the left (primary) table are included, even if there are no matches in the right (secondary) table. By mastering these concepts, you can efficiently retrieve and analyze data, build robust database applications, and maintain data integrity.
Similar Reads
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
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 fro
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 Outer Join
In SQL, joins are used to retrieve data from two or more tables based on a related column. Joins can be categorized into different types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, etc. In this article, we will learn about OUTER JOIN in PL/SQL, including its types, syntax, use cases, and ex
6 min read
PostgreSQL - LEFT JOIN
In PostgreSQL, the LEFT JOIN (or LEFT OUTER JOIN) is a powerful tool that allows you to merge data from two tables based on a related column. With a LEFT JOIN, you get all records from the "left" table and matching records from the "right" table. If thereâs no match in the right table, NULL values w
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
PL/SQL IN Operator
The PL/SQL IN operator is a powerful tool used in SQL queries to check if a value matches any value in a list or a subquery result. It simplifies querying multiple values and can make your SQL code cleaner and more readable. The IN operator is typically used in the WHERE clause to filter results bas
6 min read
Multiple Joins in SQL
Structured Query Language or SQL is a standard database language that is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, etc. In this article, we will be using the Microsoft SQL Server. Multiple Joins :Here we are going to implement the concept of multipl
3 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
SQL Natural Join
Natural join is an SQL join operation that creates a join on the base of the common columns in the tables. To perform natural join there must be one common attribute(Column) between two tables. Natural join will retrieve from multiple relations. It works in three steps. In this article, we will disc
3 min read