Chapter 4
Chapter 4
1
Outline
• Introduction
3
Fundamental Operations
(Overview)
• Unary operators (1 relation): Selection (σ),
Projection (Π), Rename (ρ).
• Notation:
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:
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
9
Rename (ρ)
• Purpose: Assigns new names to a relation and/or its
attributes.
• Notation:
Example:
10
Example
• Rename the relation Employee to Staff.
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
• 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
18
R⋈S
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
21
Outer Joins
• Left (⟕): Keep all R, pad S with NULL on no-match.
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
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
25
Full Outer Join (⟗)
• Expression:
26
Thank You!!
27