In Oracle SQL, a JOIN operation combines rows from two or more tables based on a related column between them. Joins are fundamental in SQL for querying data from multiple tables and are essential for relational databases where data is often normalized across tables to reduce redundancy. Joins allow us to retrieve a more comprehensive set of data by linking tables using a common key.
How the JOIN Operation Works
A join operation matches rows in one table with rows in another based on a condition defined in the ON
clause. The most common type of join is an equi-join, where the columns compared contain the same values. For example, a join between employees
and departments
tables could match rows based on department_id
, provided in both tables.
The basic syntax for a join operation is as follows:
Types of Joins in Oracle SQL
Oracle SQL provides several types of joins, each designed to handle specific use cases:
- Inner Join
- Left Outer Join
- Right Outer Join
- Full Outer Join
- Cross Join
- Self Join
Each type has unique characteristics, suited for different requirements in data retrieval.
1. Inner Join
An Inner Join returns only those rows that have matching values in both tables. This join type is useful when you need data that exists in both tables. If there’s no match, the row is excluded from the result.
Syntax:
Explanation: In this example, only employees with a department entry in the departments
table are included. If an employee doesn’t belong to any department, they are excluded from the results.
2. Left Outer Join
A Left Outer Join (or Left Join) returns all rows from the left table and matching rows from the right table. If there’s no match, NULL values are returned for columns in the right table.
Syntax:
Explanation: This query returns all employees, even those who don’t belong to any department. For such employees, the department_name
column will show NULL
.
3. Right Outer Join
A Right Outer Join (or Right Join) returns all rows from the right table and matching rows from the left table. If there’s no match, NULL values are returned for columns in the left table.
Syntax:
Explanation: In this query, all departments are listed, even if they don’t have any employees assigned. For departments without employees, employee columns will show NULL
.
4. Full Outer Join
A Full Outer Join returns all rows when there’s a match in either table. If there’s no match, NULL values are returned for non-matching rows in both tables. This join is useful when you want a complete view of both tables, regardless of matches.
Syntax:
Explanation: This query will include all employees and all departments. If an employee doesn’t have a department or a department doesn’t have any employees, NULL
values appear in the corresponding columns.
5. Cross Join
A Cross Join returns the Cartesian product of two tables, which means each row from the first table is combined with every row from the second table. Cross joins are rarely used as they produce large result sets, especially with large tables.
Syntax:
Explanation: This query returns every possible combination of employees and departments, leading to a very large result set.
6. Self Join
A Self Join is when a table is joined with itself. This is useful when you need to compare rows within the same table. For example, a common use case is finding managers and their direct reports within the same employees
table.
Syntax:
Explanation: Here, employees
is joined with itself to match employees with their respective managers.