Chapter2 Session3
Chapter2 Session3
1
Outline
1 Relational Algebra
3 Cartesian product
5 Set Operations
1
2
Relational Algebra
q The relational algebra consists of a set of operations
that take one or two relations as input and produce a
new relation as their result.
q Fundamental Operations
§ Unary Operation: operate on one relation
• Select σ
• Project ∏
• Rename ρ
§ Binary Operations: operate on pairs of relations
• Union ∪ , Intersection ∩
• Set different –
• Cartesian product x
• Join operations
3
Selection and Project
q Unary operation
4
Select Operation
q The select operation selects tuples that satisfy a
given predicate.
q Notation: σ p (r)
q p is called the selection predicate
5
Select Operation
Example: select those tuples of the instructor relation
where the instructor is in the “Physics” department.
Instructor relation
q Query
σ
dept_name=“Physics” (instructor)
q Result
6
Select Operation
q We allow comparisons using =, ≠, <, ≤, >, and ≥ in
the selection predicate.
q we can combine several predicates into a larger
predicate by using the connectives and (∧), or
(∨), and not (¬)
7
Select Operation
q Example: Find the instructors in Physics with a
salary greater $90,000, we write:
§ Query:
§ Result:
8
Project Operation
q Project operation selects certain columns from the
table and discards the other columns.
q Notation:
∏ A1,A2,A3 ….Ak (r)
where A1, A2, …, Ak are attribute names and r is a
relation name.
q The result is defined as the relation of k columns
obtained by erasing the columns that are not listed
q Duplicate rows removed from result, since
relations are sets
9
Project Operation
q Example: eliminate the dept_name attribute
of instructor relation
§ Query
∏ID, name, salary (instructor)
§ Result:
10
Composition of Relational
Operations
q The result of a relational-algebra operation is
relation and therefore of relational-algebra
operations can be composed together into a
relational-algebra expression.
q Consider the query -- Find the names of all
instructors in the Physics department.
12
Cartesian-Product Operation
q Example: the Cartesian product of the relations
instructor and teaches is written as:
instructor X teaches
instructor
teaches
13
Cartesian-Product Operation
q Result:
14
Cartesian-Product Operation
q We construct a tuple of the result out of each
possible pair of tuples: one from the instructor
relation and one from the teaches relation.
q Since the instructor ID appears in both relations
we distinguish between these attribute by
attaching to the attribute the name of the relation
from which the attribute originally came.
§ instructor.ID
§ teaches.ID
q Assume that we have n1 tuples in instructor and n2
tuples in teaches, so there are n1 ∗ n2 tuples in r
15
Join Operation
q The Cartesian-Product
instructor X teaches
associates every tuple of instructor with every
tuple of teaches.
q Most of the resulting rows have information
about instructors who did NOT teach a particular
course.
16
Join Operation
q To get only those tuples of “instructor X teaches”
that pertain to instructors and the courses that they
taught, we write:
σ instructor.id = teaches.id (instructor x teaches ))
17
Join Operation
q The table corresponding to:
σ instructor.id = teaches.id (instructor x teaches))
18
Join Operation
19
Join Operation
20
Join operator
21
Outer Joins
q In Inner Join, we matched rows are returned and
unmatched rows are not returned.
q But, in outer join, we include those tuples which
meet the given condition along with that, we also
add those tuples which do not meet the required
condition.
q There are three types of outer joins:
§ Left outer join
§ Right outer join
§ Full outer join
22
Outer Joins
23
Left Outer Join
Left Outer Join : A <join condition> B
q Ensures that all tuples in the in the relation A are
present in the result set.
q The tuples in A without matching tuples in B are
filled with null values for B’s attributes
24
Left Outer Join example
Students Courses
stud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter EN CH Chemistry
Students <course = course#> Courses
Result:
stud# Students.name course course# Courses.name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter EN NULL NULL
25
Right Outer Join
Right Outer Join: A <join condition> B
26
Right Outer Join example
Students Courses
stud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter EN CH Chemistry
Students <course = course#> Courses
Result:
stud# Students.name course course# Courses.name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
NULL NULL NULL CH Chemistry
27
Full Outer Join
Full Outer Join: A <join condition> B
q Ensures that all tuples of A and B are present in
the result set
28
Full Outer Join Example
Students Courses
stud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter EN CH Chemistry
Students <course = course#> Courses
Result:
stud# Students.name course course# Courses.name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter CN NULL NULL
NULL NULL NULL CH Chemistry
29
Set operations
q Example
Section relation
30
Union Operation
q The union operation allows us to combine two relations
q Notation: r ∪ s
q For r ∪ s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd
column of r deals with the same type of values as does the
2nd column of s)
31
Union Operation
q Example: to find all courses taught in the Fall 2017
semester, or in the Spring 2018 semester, or in both
∏course_id (σ semester=“Fall” Λ year=2017 (section)) ∪
∏course_id (σ semester=“Spring”
Λ year=2018 (section))
32
Union Operation
section relation result
33
Set-Intersection Operation
q The set-intersection or intersection operation
allows us to find tuples that are in both the
input relations.
q Notation: r ∩ s
q Assume:
§ r, s have the same arity
§ attributes of r and s are compatible
34
Set-Intersection Operation
q Example: Find the set of all courses taught in
both the Fall 2017 and the Spring 2018
semesters.
∏course_id (σ semester=“Fall” Λ year=2017 (section)) ∩
∏course_id (σ semester=“Spring” Λ year=2018 (section))
q Result:
35
Set Difference Operation
q The set-difference operation allows us to find
tuples that are in one relation but are not in
another.
q Notation r – s
q Set differences must be taken between
compatible relations.
§ r and s must have the same arity
§ attribute domains of r and s must be compatible
36
Set Difference Operation
q Example: to find all courses taught in the Fall
2017 semester, but not in the Spring 2018
semester
∏course_id (σ semester=“Fall” Λ year=2017 (section)) −
∏course_id (σ semester=“Spring” Λ year=2018 (section))
q Result:
37
The Rename Operation
q The results of relational-algebra expressions do
not have a name that we can use to refer to them.
The rename operator, ρ , is provided for that
purpose
q The expression:
ρx (E)
returns the result of expression E under the name x
q Another form of the rename operation:
ρx(A1,A2, .. An) (E)
38
Equivalent Queries
q There is more than one way to write a query in
relational algebra.
q Example: Find information about courses taught by
instructors in the Physics department with salary greater
than 90,000
q Query 1
σ dept_name=“Physics” ∧ salary > 90,000 (instructor)
q Query 2
σ dept_name=“Physics” (σ salary > 90.000 (instructor))
40
Exercises
Example: Customer
q Ex1:
41
Exercises sid bid day
Reserves 22 101 10/10/11
q Ex2 58 103 11/12/11
sid sname rating age
bid bname color
22 Jesly 7 45.0 101 Interlake Blue
31 Mishail 8 55.5 102 Interlake Red
58 Raj 10 35.0 103 Clipper Green
sailors 104 Marine Red
Boats
1.Find names of sailors who’ve reserved boat #103
2.Find names of sailors who’ve reserved a red boat
3.Find sailors who’ve reserved a red or a green boat
4.Find sailors who’ve reserved a red and a green boat
5. Find the names of sailors who’ve reserved all boats
42
43