A join operation in relational algebra combines rows from two or more relations based on a related column between them. Joins are essential in relational databases for combining data from multiple tables based on logical relationships between them. There are several types of joins in relational algebra, each with different behaviors for matching rows.
Here are the primary types of joins:
Let's discuss each of these types in detail:
An Inner Join (often just called "Join") returns only the rows where there is a match between the two relations based on a specified condition. It combines rows from two relations if the condition (usually an equality between specified attributes) is satisfied. If there is no match, the row is not included in the result.
R ⨝ SConsider two relations, Employees and Departments:
Employees:
| EmployeeID | Name | DeptID |
|---|---|---|
| 1 | Alice | D1 |
| 2 | Bob | D2 |
| 3 | Charlie | D3 |
Departments:
| DeptID | DeptName |
|---|---|
| D1 | HR |
| D2 | IT |
| D4 | Finance |
Inner Join on DeptID:
Employees ⨝ Departments
Result:
| EmployeeID | Name | DeptID | DeptName |
|---|---|---|---|
| 1 | Alice | D1 | HR |
| 2 | Bob | D2 | IT |
DeptID matches in both relations are included in the result.DeptID = D3 in Employees and DeptID = D4 in Departments are not included because there is no matching department for them.A Natural Join is a specific type of inner join that automatically matches columns with the same name in both relations. It eliminates duplicate columns from the result.
R ⨝ SConsider the following relations:
Employees:
| EmployeeID | Name | DeptID |
|---|---|---|
| 1 | Alice | D1 |
| 2 | Bob | D2 |
| 3 | Charlie | D3 |
Departments:
| DeptID | DeptName |
|---|---|
| D1 | HR |
| D2 | IT |
| D4 | Finance |
A Natural Join on DeptID will be:
Employees ⨝ Departments
Result:
| EmployeeID | Name | DeptID | DeptName |
|---|---|---|---|
| 1 | Alice | D1 | HR |
| 2 | Bob | D2 | IT |
DeptID column and removes the duplicate DeptID column from the result.A Theta Join is a join where the condition is not restricted to equality. Instead, it can be any condition based on comparison operators like =, >, <, >=, <=, !=, etc.
R ⨝θ S, where θ is the condition (e.g., =, <, >, etc.).Consider the following relations:
Employees:
| EmployeeID | Name | Salary |
|---|---|---|
| 1 | Alice | 50000 |
| 2 | Bob | 60000 |
| 3 | Charlie | 70000 |
Departments:
| DeptID | SalaryLimit |
|---|---|
| D1 | 60000 |
| D2 | 65000 |
A Theta Join on the condition Salary > SalaryLimit would be:
Employees ⨝ (Salary > SalaryLimit) Departments
Result:
| EmployeeID | Name | Salary | DeptID | SalaryLimit |
|---|---|---|---|---|
| 2 | Bob | 60000 | D1 | 60000 |
| 3 | Charlie | 70000 | D2 | 65000 |
SalaryLimit of the department.An Equi Join is a type of theta join where the condition is specifically an equality condition (=) between columns from two relations.
R ⨝ (R.A = S.B) SUsing the Employees and Departments relations:
Employees ⨝ (Employees.DeptID = Departments.DeptID) Departments
Result:
| EmployeeID | Name | DeptID | DeptName |
|---|---|---|---|
| 1 | Alice | D1 | HR |
| 2 | Bob | D2 | IT |
Outer Joins include Left Outer Join, Right Outer Join, and Full Outer Join. These joins return all tuples from one relation, and the matching tuples from the other relation. If there is no match, the result will include NULL values for the attributes of the relation that does not have a matching row.
The Left Outer Join returns all rows from the left relation and the matched rows from the right relation. If there is no match, the result will contain NULL for columns from the right relation.
R ⟕ SNULL values are returned for the right relation's columns.Employees ⟕ Departments
Result:
| EmployeeID | Name | DeptID | DeptName |
|---|---|---|---|
| 1 | Alice | D1 | HR |
| 2 | Bob | D2 | IT |
| 3 | Charlie | D3 | NULL |
D3 is not in the Departments table, the DeptName is NULL for Charlie.The Right Outer Join is similar to the left outer join, but it returns all rows from the right relation and the matched rows from the left relation. If no match exists, NULL values are used for columns from the left relation.
R ⟖ SNULL.Employees ⟖ Departments
Result:
| EmployeeID | Name | DeptID | DeptName |
|---|---|---|---|
| 1 | Alice | D1 | HR |
| 2 | Bob | D2 | IT |
| NULL | NULL | D4 | Finance |
D4 appears in the result even though no employee is associated with it, with NULL values for the employee's columns.The Full Outer Join returns all rows from both relations. If there is no match, NULL values are placed in the columns of the relation with no matching tuple.
R ⟗ SEmployees ⟗ Departments
Result:
| EmployeeID | Name | DeptID | DeptName |
|---|---|---|---|
| 1 | Alice | D1 | HR |
| 2 | Bob | D2 | IT |
| 3 | Charlie | D3 | NULL |
| NULL | NULL | D4 | Finance |
NULL values are placed where there are no matches.A Self Join is a join where a relation is joined with itself. This is useful when you need to compare rows within the same relation, such as finding employees who work in the same department.
R ⨝ REmployees ⨝ (Employees.DeptID = Employees.DeptID) Employees
This operation would compare all employees with each other who belong to the same department.
NULL values.Each type of join has its specific use case depending on the nature of the data and the relationship between the tables.
Open this section to load past papers