How is the JOIN statement used in Oracle?

In Oracle, JOIN is used to connect the data from multiple tables. This operation matches rows between two or more tables based on shared columns (typically primary and foreign keys) and returns a result set containing matched rows.

In Oracle, there are several different JOIN types that can be used:

  1. Inner join returns the intersection of rows that match between two tables, only returning those rows that exist in both tables.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
  1. Outer join: Returns the union of matching rows from two tables, as well as rows that do not have a match. Outer joins are divided into left outer joins and right outer joins.

A left outer join returns all rows from the left table and only the matching rows from the right table.

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Return all rows from the right table along with the rows from the left table that match the right table.

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
  1. Self-join: Connecting a table with itself to search for rows with similar attributes within the table.
SELECT column_name(s)
FROM table1 t1, table1 t2
WHERE t1.column_name = t2.column_name;
  1. Cross join: Returns the Cartesian product of two tables, meaning every row from one table is combined with every row from the other table.
SELECT column_name(s)
FROM table1
CROSS JOIN table2;

Here are some common usage of JOIN in Oracle, you can choose the appropriate JOIN type based on your specific needs.

bannerAds