0% found this document useful (0 votes)
4 views27 pages

Chapter 4

Chapter 4 covers Relational Algebra, a procedural query language for manipulating relations through fundamental operations such as selection, projection, and various types of joins. It explains unary and binary operators, including Cartesian product, union, intersection, and set difference, along with their notations and examples. The chapter also details outer joins and their variations, illustrating how to handle cases with unmatched records.

Uploaded by

Rohobot
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)
4 views27 pages

Chapter 4

Chapter 4 covers Relational Algebra, a procedural query language for manipulating relations through fundamental operations such as selection, projection, and various types of joins. It explains unary and binary operators, including Cartesian product, union, intersection, and set difference, along with their notations and examples. The chapter also details outer joins and their variations, illustrating how to handle cases with unmatched records.

Uploaded by

Rohobot
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/ 27

Chapter 4: Relational Algebra

1
Outline

• Introduction

• Fundamental Operations (Overview)

• Selection (σ), Projection (Π), Rename (ρ)

• Cartesian Product (×), Union (∪), Intersection (∩), Set


Difference (−), Natural Join (⋈), Theta Join (⋈_θ),
Assignment (←), Outer Joins
2
Introduction

• Relational Algebra is a procedural query language over


relations.

• Inputs/Output: Takes one or two relations as input, produces


a new relation.

• Expressions: Sequences of operations form a


relational‑algebra expression tree.

3
Fundamental Operations
(Overview)
• Unary operators (1 relation): Selection (σ),
Projection (Π), Rename (ρ).

• Binary (set) operators (2 relations): Cartesian


Product (×), Union (∪), Difference (−).

• Set‑theoretic roots: Union and difference require


union compatibility.
4
Selection (σ)
• Purpose: Filters rows (tuples) by a Boolean predicate.

• Notation:

Example: From Employees pick those over age 50:

EmpID Name Age


1 Alice Chen 29
2 Bob Singh 52
3 Carol Ng 42

5
• yields only employees whose Age attribute is greater
than 50.
EmpID Name Age
2 Bob Singh 52

6
Projection (Π)
• Purpose: Picks columns (attributes) from a relation

• Notation:

• Example: From Employees get only names and


salaries:

EmpID (PK) Name Salary Department


1 Alice Chen 60,000 HR
2 Bob Singh 75,000 IT
3 Carol Ng 82,500 Finance

7
Name Salary
Alice Chen 60,000
Bob Singh 75,000
Carol Ng 82,500

8
Example
• Original Relation: Employees
EmpID (PK) Name Age Department Salary
1 Alice Chen 29 HR 60,000
2 Bob Singh 35 IT 75,000
3 Carol Ng 42 Finance 82,500
4 Daniel Abebe 31 Marketing 68,000
5 Eva Teklu 27 Operations 58,500

• Resulting Relation: SeniorSal

EmpID Name Salary


2 Bob Singh 75,000
3 Carol Ng 82,500
4 Daniel Abebe 68,000

9
Rename (ρ)
• Purpose: Assigns new names to a relation and/or its
attributes.

• Notation:

Example:

• renames Employees to NewEmp, and attribute EmpId to


ID, Name to FullName etc.

10
Example
• Rename the relation Employee to Staff.

EmpID Name Dept Salary


1 Alice Chen HR 60,000
2 Bob Singh IT 75,000
3 Carol Ng Finance 82,500

StaffID Name Department Salary


1 Alice Chen HR 60,000
2 Bob Singh IT 75,000
3 Carol Ng Finance 82,500

11
Cartesian Product (×)
• Purpose: Pairs every tuple of R with every tuple of S.
• Notation:
• R×S
• Example:
• R(A,B) = {(1,2),(3,4)}
• S(C,D) = {(‘a’,10),(‘b’,20)}
• R×S = {(1,2,a,10),(1,2,b,20),(3,4,a,10),(3,4,b,20)}

12
Union (∪)
• Purpose: All tuples in R or S (duplicates removed).
• Notation:
• R∪S
• Example:
• R = {(1,‘Alice’),(2,‘Bob’)}
• S = {(2,‘Bob’),(3,‘Carol’)}
• R ∪ S = {(1,‘Alice’),(2,‘Bob’),(3,‘Carol’)}

13
Intersection (∩)
• Purpose: Tuples common to both R and S.
• Notation:
R∩S
• Example:
• With R and S above,
• R = {(1,‘Alice’),(2,‘Bob’)}
• S = {(2,‘Bob’),(3,‘Carol’)}
• R ∩ S = {(2,‘Bob’)}

14
Set Difference (−)
• Purpose: Tuples in R but not in S.
• Notation:
R−S
• Example:
• R = {(1,‘Alice’),(2,‘Bob’)}
• S = {(2,‘Bob’),(3,‘Carol’)}
• R - S = {(1,‘Alice’)}

15
R S R×S
R.A R.B S.A S.B
A B A B
1 “a” 2 “b”
1 “a” 2 “b” 1 “a” 3 “c”
2 “b” 2 “b”
2 “b” 3 “c” 2 “b” 3 “c”

R∪S
R−S
A B
A B
1 “a”
2 “b” 1 “a”
3 “c”
S−R
R∩S
A B
A B
3 “c”
2 “b”

16
Union Compatibility
• Definition: R(A₁…Aₙ), S(B₁…Bₙ) are compatible if:
• Same number of attributes n

• Domain(Aᵢ) = Domain(Bᵢ) for all I

• U, n & -.

17
Natural Join (⋈)
• Purpose: Equi‑join on all common attributes, then
remove duplicates
• Notation: R⋈S S
R
EmpID Name DeptID DeptID DeptName Location
1 Alice 10
Human Re
2 Bob 20 10 HQ
sources
3 Carol 10
20 IT Remote
4 Dave 30
40 Marketing HQ

Since DeptID is common to both, natural join will:

1.Match rows where R.DeptID = S.DeptID


2.Include each common attribute only once in the result
schema.

18
R⋈S

EmpID Name DeptID DeptName Location


Human Resour
1 Alice 10 HQ
ces
Human Resour
3 Carol 10 HQ
ces
2 Bob 20 IT Remote

19
Theta Join (⋈_θ)
• Purpose: Join on arbitrary condition θ.
• Notation: DeptID DeptName Location
EmpID Name DeptID 10 HR HQ
1 Alice 10
20 IT Remote
2 Bob 20
3 Carol 10 40 Marketing HQ
4 Dave 30

EmpID Name R.DeptID S.DeptID DeptName Location


1 Alice 10 20 IT Remote
1 Alice 10 40 Marketing HQ
2 Bob 20 40 Marketing HQ
3 Carol 10 20 IT Remote
3 Carol 10 40 Marketing HQ
4 Dave 30 40 Marketing HQ
20
Assignment (←)
• Purpose: Store intermediate result for reuse
• Notation:
• X←(expression)
• Example:

21
Outer Joins
• Left (⟕): Keep all R, pad S with NULL on no-match.

• Right (⟖): Keep all S, pad R with NULL.

• Full (⟗): Keep all R ∪ S, pad NULL where needed.

• Example: List all Projects and Teams—even projects


with no teams.

22
Examples: Outer Join
• Employees
EmpID Name DeptID
1 Alice 10
2 Bob 20
3 Carol 30

• Departments
DeptID DeptName
10 HR
20 IT
40 Marketing

23
Left Outer Join (⟕)
• Expression:
• Keeps all rows from Employees, and fills NULLs where no
matching DeptID in Departments.
EmpID Name DeptID DeptName
1 Alice 10 HR
2 Bob 20 IT
3 Carol 30 NULL

• Carol's department (DeptID 30) has no match in


Departments, so DeptName is NULL.

24
Right Outer Join (⟖)
• Expression:
• Keeps all rows from Employees, and fills NULLs where
no matching DeptID in Departments.
EmpID Name DeptID DeptName
1 Alice 10 HR
2 Bob 20 IT
NULL NULL 40 Marketing

• DeptID 40 in Departments has no match in Employees,


so EmpID and Name are NULL.

25
Full Outer Join (⟗)
• Expression:

• Combines results of both left and right outer joins—keeps


all rows from both tables.
EmpID Name DeptID DeptName
1 Alice 10 HR
2 Bob 20 IT
3 Carol 30 NULL
NULL NULL 40 Marketing

26
Thank You!!

27

You might also like