What are different types of Joins in ORACLE?
Oracle JOINS are used to retrieve data from multiple tables. An Oracle JOIN is performed whenever two or more tables are joined in a SQL statement. There are different types of Oracle joins:
- ORACLE Cross Join(Or Sometimes called Cartesian Join)
- ORACLE INNER JOIN(Or Sometimes called Simple Join)
- ORACLE OUTER JOIN
- LEFT OUTER JOIN (Or Sometimes called LEFT JOIN)
- RIGHT OUTER JOIN (Or Sometimes called RIGHT JOIN)
- FULL OUTER JOIN (Or Sometimes called FULL JOIN)
Cross Join (Cartesian join)
Cross join will give m*n rows where- m (Number of rows in left table) and n (Number of rows in right table)
Select * from Employee Cross Join Department;
Or
Select * from Employee, Department;
INNER JOIN (SIMPLE JOIN)
It is the most common type of join. Oracle INNER JOINS return all rows from multiple tables where the join condition is met.
Syntax
The syntax for the INNER JOIN in Oracle/PL SQL is:
SELECT Columns
FROM Table1
INNER JOIN Table2
ON Table1.Column = Table2.Column;
Visual Illustration
In this visual diagram, the Oracle INNER JOIN returns the shaded area:
The Oracle INNER JOIN would return the records where Table1 and Table2 intersect.
Example
Here is an example of an Oracle INNER JOIN:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
This Oracle INNER JOIN example would return all rows from the suppliers and orders tables where there is a matching supplier_id value in both the suppliers and orders tables.
Let's look at some data to explain how the INNER JOINS works:
We have a table called suppliers with two fields (supplier_id and supplier_name). It contains the following data:
Supplier_id
|
Supplier_name
|
10000
|
IBM
|
10001
|
Hewlett Packard
|
10002
|
Microsoft
|
10003
|
NVIDIA
|
We have another table called orders with three fields (order_id, supplier_id, and order_date). It contains the following data:
Order_id
|
Supplier_id
|
Order_date
|
500125
|
10000
|
2003/05/12
|
500126
|
10001
|
2003/05/13
|
500127
|
10004
|
2003/05/14
|
If we run the Oracle SELECT statement (that contains an INNER JOIN) below:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
Our result set would look like this:
supplier_id
|
Name
|
order_date
|
10000
|
IBM
|
2003/05/12
|
10001
|
Hewlett Packard
|
2003/05/13
|
The rows for Microsoft and NVIDIA from the supplier table would be omitted, since the supplier_id's 10002 and 10003 do not exist in both tables. The row for 500127 (order_id) from the orders table would be omitted, since the supplier_id 10004 does not exist in the suppliers table.
Old Syntax
As a final note, it is worth mentioning that the Oracle INNER JOIN example above could be rewritten using the older implicit syntax as follows (but we still recommend using the INNER JOIN keyword syntax):
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
OUTER JOINS- Are the following:
LEFT OUTER JOIN
Another type of join is called an Oracle LEFT OUTER JOIN. This type of join returns all rows from the LEFT-hand table specified in the ON condition and only those rows from the other table where the joined fields are equal (join condition is met).
Syntax
The syntax for the Oracle LEFT OUTER JOIN is:
SELECT columns
FROM Table1
LEFT [OUTER] JOIN Table2
ON Table1.column = Table2.column;
In some databases, the LEFT OUTER JOIN keywords are replaced with LEFT JOIN.
Visual Illustration
In this visual diagram, the Oracle LEFT OUTER JOIN returns the shaded area:
The Oracle LEFT OUTER JOIN would return the all records from Table1 and only those records from Table2 that intersect with Table1.
Example
Here is an example of an Oracle LEFT OUTER JOIN:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
LEFT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
This LEFT OUTER JOIN example would return all rows from the suppliers table and only those rows from the orders table where the joined fields are equal.
If a supplier_id value in the suppliers table does not exist in the orders table, all fields in the orders table will display as <null> in the result set.
Let's look at some data to explain how LEFT OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and supplier_name). It contains the following data:
supplier_id
|
supplier_name
|
10000
|
IBM
|
10001
|
Hewlett Packard
|
10002
|
Microsoft
|
10003
|
NVIDIA
|
We have a second table called orders with three fields (order_id, supplier_id, and order_date). It contains the following data:
order_id
|
supplier_id
|
order_date
|
500125
|
10000
|
2003/05/12
|
500126
|
10001
|
2003/05/13
|
If we run the SELECT statement (that contains a LEFT OUTER JOIN) below:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
LEFT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
Our result set would look like this:
supplier_id
|
supplier_name
|
order_date
|
10000
|
IBM
|
2003/05/12
|
10001
|
Hewlett Packard
|
2003/05/13
|
10002
|
Microsoft
|
<null>
|
10003
|
NVIDIA
|
<null>
|
The rows for Microsoft and NVIDIA would be included because a LEFT OUTER JOIN was used. However, you will notice that the order_date field for those records contains a <null> value.
Old Syntax
As a final note, it is worth mentioning that the LEFT OUTER JOIN example above could be rewritten using the older implicit syntax that utilizes the outer join operator (+) as follows (but we still recommend using the LEFT OUTER JOIN keyword syntax):
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id(+);
Another Example-
RIGHT OUTER JOIN
Another type of join is called an Oracle RIGHT OUTER JOIN. This type of join returns all rows from the RIGHT-hand table specified in the ON condition and only those rows from the other table where the joined fields are equal (join condition is met).
Syntax
The syntax for the Oracle RIGHT OUTER JOIN is:
SELECT columns
FROM Table1
RIGHT [OUTER] JOIN Table2
ON Table1.Column = Table2.Column;
In some databases, the RIGHT OUTER JOIN keywords are replaced with RIGHT JOIN.
Visual Illustration
In this visual diagram, the Oracle RIGHT OUTER JOIN returns the shaded area:
The Oracle RIGHT OUTER JOIN would return the all records from Table2 and only those records from Table1 that intersect with Table2.
Example
Here is an example of an Oracle RIGHT OUTER JOIN:
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers
RIGHT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
This RIGHT OUTER JOIN example would return all rows from the orders table and only those rows from the suppliers table where the joined fields are equal.
If a supplier_id value in the orders table does not exist in the suppliers table, all fields in the suppliers table will display as <null> in the result set.
Let's look at some data to explain how RIGHT OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and supplier_name). It contains the following data:
supplier_id
|
supplier_name
|
10000
|
Apple
|
10001
|
Google
|
We have a second table called orders with three fields (order_id, supplier_id, and order_date). It contains the following data:
order_id
|
supplier_id
|
order_date
|
500125
|
10000
|
2013/08/12
|
500126
|
10001
|
2013/08/13
|
500127
|
10002
|
2013/08/14
|
If we run the SELECT statement (that contains a RIGHT OUTER JOIN) below:
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers
RIGHT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
Our result set would look like this:
order_id
|
order_date
|
supplier_name
|
500125
|
2013/08/12
|
Apple
|
500126
|
2013/08/13
|
Google
|
500127
|
2013/08/14
|
<null>
|
The row for 500127 (order_id) would be included because a RIGHT OUTER JOIN was used. However, you will notice that the supplier_name field for that record contains a <null> value.
Old Syntax
As a final note, it is worth mentioning that the RIGHT OUTER JOIN example above could be rewritten using the older implicit syntax that utilizes the outer join operator (+) as follows (but we still recommend using the RIGHT OUTER JOIN keyword syntax):
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers, orders
WHERE suppliers.supplier_id(+) = orders.supplier_id;
ANOTHER EXAMPLE-
FULL OUTER JOIN
Another type of join is called an Oracle FULL OUTER JOIN. This type of join returns all rows from the LEFT-hand table and RIGHT-hand table with nulls in place where the join condition is not met.
Syntax
The syntax for the Oracle FULL OUTER JOIN is:
SELECT columns
FROM Table1
FULL [OUTER] JOIN Table2
ON Table1.Column = Table2.Column;
In some databases, the FULL OUTER JOIN keywords are replaced with FULL JOIN.
Visual Illustration
In this visual diagram, the Oracle FULL OUTER JOIN returns the shaded area:
The Oracle FULL OUTER JOIN would return the all records from both Table1 and Table2.
Example
Here is an example of an Oracle FULL OUTER JOIN:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
FULL OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
This FULL OUTER JOIN example would return all rows from the suppliers table and all rows from the orders table and whenever the join condition is not met, <nulls> would be extended to those fields in the result set.
If a supplier_id value in the suppliers table does not exist in the orders table, all fields in the orders table will display as <null> in the result set. If a supplier_id value in the orders table does not exist in the suppliers table, all fields in the suppliers table will display as <null> in the result set.
Let's look at some data to explain how FULL OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and supplier_name). It contains the following data:
supplier_id
|
supplier_name
|
10000
|
IBM
|
10001
|
Hewlett Packard
|
10002
|
Microsoft
|
10003
|
NVIDIA
|
We have a second table called orders with three fields (order_id, supplier_id, and order_date). It contains the following data:
order_id
|
supplier_id
|
order_date
|
500125
|
10000
|
2013/08/12
|
500126
|
10001
|
2013/08/13
|
500127
|
10004
|
2013/08/14
|
If we run the SELECT statement (that contains a FULL OUTER JOIN) below:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
FULL OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
Our result set would look like this:
supplier_id
|
supplier_name
|
order_date
|
10000
|
IBM
|
2013/08/12
|
10001
|
Hewlett Packard
|
2013/08/13
|
10002
|
Microsoft
|
<null>
|
10003
|
NVIDIA
|
<null>
|
<null>
|
<null>
|
2013/08/14
|
The rows for Microsoft and NVIDIA would be included because a FULL OUTER JOIN was used. However, you will notice that the order_date field for those records contains a <null> value.
The row for supplier_id 10004 would be also included because a FULL OUTER JOIN was used. However, you will notice that the supplier_id and supplier_name field for those records contain a <null> value.
Old Syntax
As a final note, it is worth mentioning that the FULL OUTER JOIN example above could not have been written in the old syntax without using a UNION query.
ANOTHER EXAMPLE-
What will be the result of different
joins on below two tables?
TableA
|
TableB
| |
ColA
|
ColB
| |
1
|
1
| |
1
|
1
| |
1
|
1
| |
1
|
1
|
In
all the joins result will be same as the column values are same in both the
tables. Each value from TableA will match with the TableB and fetch the result
set. In this case result set will give m*n (16) rows in all type of joins.
Get involved
and leave your Comments in the Box Below. The more people get involved, the
more we all benefit. So, leave your thoughts before you leave
the page.
Nice Information.Thanks for sharing. Do check out our blog-we are the best oracle software service frisco
ReplyDeleteThis is a very nice article.
ReplyDeleteOracle Fusion HCM Training
This is a very big and very important subject that you have shared with us, thank you very much for that.
ReplyDeletewebsite development company in Surat Gujarat
Discover Seekware, the foremost Blockchain AI blockchain development company at the forefront of innovation and technology. Our expert team leverages cutting-edge artificial intelligence and blockchain solutions to transform businesses across industries. From developing secure and efficient decentralized applications to implementing smart contracts, Seekware leads the way in harnessing the power of AI and blockchain for your organization's success. Explore our comprehensive services and embark on a journey towards a smarter, more secure future with Seekware.
ReplyDeleteWelcome to Trendy Mixup, your ultimate destination for discovering a captivating blend of the latest fashion, lifestyle, and cultural trends! Explore our curated collection of unique mixes, where style meets innovation, and tradition intertwines with modernity. From fashion-forward apparel to cutting-edge accessories, we bring you a diverse range of products that encapsulate the essence of contemporary trends and timeless classics. Stay ahead of the curve and indulge in a fusion of creativity and sophistication with Trendy Mixup. Join us on a journey where every mix tells a unique story of trendsetting allure and vibrant expression.
ReplyDeleteHorror fans, rejoice! Amazon Prime Video is a treasure trove of spine-tingling, nail-biting horror films, and 2023 has been no exception. From chilling classics to fresh, innovative releases, there’s something to satisfy every horror aficionado’s craving for a good scare. So, grab your popcorn, dim the lights, and get ready to scream with our picks for the best horror movies on Amazon Prime in 2023.
ReplyDelete