0% found this document useful (0 votes)
11 views16 pages

Joins

The document explains various types of joins in relational algebra, including Natural Join, Theta Join, Equi Join, and Outer Joins (Left, Right, and Full). Each join type is defined with SQL and PQL examples, illustrating how they combine tables based on specific conditions or equality. The document also summarizes the inclusion of matching and non-matching rows for each join type.

Uploaded by

c4x6vgk2n7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

Joins

The document explains various types of joins in relational algebra, including Natural Join, Theta Join, Equi Join, and Outer Joins (Left, Right, and Full). Each join type is defined with SQL and PQL examples, illustrating how they combine tables based on specific conditions or equality. The document also summarizes the inclusion of matching and non-matching rows for each join type.

Uploaded by

c4x6vgk2n7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

DBMS

Joins in Relational
Algebra

Understanding Inner and Outer Joins


Natural Join
• Natural Join combines the tables based on
columns with the same name and data type.

• Automatically matches common columns


between tables.

• SQL: SELECT * FROM Employees NATURAL


JOIN Departments;

• PQL: Employees ⋈ Departments


Example - Natural Join
emp_id name dept_id
dept_id dept_name
101 Alice 1
102 Bob 2 1 HR
103 Charlie 1 2 Engineering
104 David 3 3 Marketing
105 Eva NULL 4 Finance

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

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

emp_id name dept_id dept_id dept_name


101 Alice 1 1 HR
102 Bob 2 2 Engineering
103 Charlie 1 1 HR
104 David 3 3 Marketing

Employees ⋈(E.dept_id = D.dept_id) Departments


Outer Joins
• Outer Joins Returns matching rows plus
unmatched rows from one or both tables.

• 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

emp_id name dept_id dept_id dept_name


101 Alice 1 1 HR
102 Bob 2 2 Engineering
103 Charlie 1 1 HR
104 David 3 3 Marketing
105 Eva NULL NULL NULL

Employees ⟕ (E.dept_id = D.dept_id) Departments


Right Outer Join
• It returns all rows from the right table and
matching rows from the left table.
SQL-
SELECT * FROM Employees E RIGHT OUTER JOIN
Departments D ON E.dept_id = D.dept_id;

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

emp_id name dept_id dept_id dept_name


101 Alice 1 1 HR
103 Charlie 1 1 HR
102 Bob 2 2 Engineering
104 David 3 3 Marketing
NULL NULL NULL 4 Finance

Employees ⟖ (E.dept_id = D.dept_id) Departments


Full Outer Join
• It returns all rows when there's a match in
either the left or right table.
SQL –
SELECT * FROM Employees E FULL OUTER JOIN
Departments D ON E.dept_id = D.dept_id;

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

Employees ⟗ (E.dept_id = D.dept_id) Departments


Includes Includes Non-
Join Type Definition Matching Matching Rows?
Rows?

Natural Join Joins based on common column(s) with the Yes No


same name and data type.
Joins based on a condition other than
Theta Join equality (e.g., <, >, !=). Yes No

Joins based on equality between specific


Equi Join columns. Yes No

Returns all rows from the left table, plus


Left Outer matching rows from the right table. Non- Yes Left table only
Join matching right table values are filled with
NULL.

Returns all rows from the right table, plus


Right Outer matching rows from the left table. Non- Yes Right table only
Join matching left table values are filled with
NULL.

Returns all rows when there is a match in


Full Outer either table. Non-matching values from both Yes Both tables
Join tables are filled with NULL.
Thank You

You might also like