Joins
Joins
Joins in Relational
Algebra
Employees Departments
Employees ⋈ Departments
Theta Join
• Theta Join combines the tables based on a
specified condition (not necessarily equality).
• Conditional Operators incudes: >,<, <=, >= etc.
SQL –
SELECT * FROM Employees E, Departments D
WHERE E.dept_id < D.dept_id;
PQL-
Employees⋈(E.dept_id<D.dept_id) Departments
Example - Theta Join
emp_id name dept_id dept_id dept_name
101 Alice 1 1 HR
102 Bob 2 2 Engineering
103 Charlie 1
3 Marketing
105 Eva NULL
4 Finance
Employees Departments
emp_id name dept_id dept_id dept_name
101 Alice 1 2 Engineering
101 Alice 1 3 Marketing
101 Alice 1 4 Finance
102 Bob 2 3 Marketing
102 Bob 2 4 Finance
103 Charlie 1 2 Engineering
103 Charlie 1 3 Marketing
103 Charlie 1 4 Finance
Employees ⋈(E.dept_id < D.dept_id) Departments
Equi Join
• Joins tables based on equality between
specified columns.
SQL-
SELECT * FROM Employees E, Departments D
WHERE E.dept_id =
D.dept_id;
PQL-
Employees⋈(E.dept_id=D.dept_id) Departments
Example - Equi Join
emp_id name dept_id
dept_id dept_name
101 Alice 1
1 HR
102 Bob 2
2 Engineering
103 Charlie 1
3 Marketing
104 David 3
4 Finance
105 Eva NULL
Employees Departments
• Types:
1. Left Outer Join
2. Right Outer Join
3. Full Outer Join
Left Outer Join
• It returns all rows from the left table and
matching rows from the right table.
SQL-
SELECT * FROM Employees E LEFT OUTER JOIN
Departments D ON E.dept_id = D.dept_id;
PQL-
π*(Employees ⟕ (E.dept_id=D.dept_id)Departments)
Example – Left Outer Join
emp_id name dept_id
dept_id dept_name
101 Alice 1
1 HR
102 Bob 2
2 Engineering
103 Charlie 1
3 Marketing
104 David 3
4 Finance
105 Eva NULL
Employees Departments
PQL-
π* (Employees ⟖ (E.dept_id=D.dept_id) Departments)
Example – Right Outer Join
emp_id name dept_id
dept_id dept_name
101 Alice 1
1 HR
102 Bob 2
2 Engineering
103 Charlie 1
3 Marketing
104 David 3
4 Finance
105 Eva NULL
Employees Departments
PQL –
π*(Employees ⟗ (E.dept_id=D.dept_id) Departments)
Example – Full Outer Join
emp_id name dept_id
dept_id dept_name
101 Alice 1
1 HR
102 Bob 2
2 Engineering
103 Charlie 1
3 Marketing
104 David 3
4 Finance
105 Eva NULL
Employees Departments
emp_id name dept_id dept_name
101 Alice 1 HR
102 Bob 2 Engineering
103 Charlie 1 HR
104 David 3 Marketing
105 Eva NULL NULL
NULL NULL 4 Finance