Relational Algebra
Relational Algebra
Relational Algebra
Database System Concepts, 7th Ed.
©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Relational Algebra
Database System Concepts - 7th Edition 2.2 ©Silberschatz, Korth and Sudarshan
Relational Algebra
Database System Concepts - 7th Edition 2.3 ©Silberschatz, Korth and Sudarshan
Select Operation
▪ Notation: p (r)
Database System Concepts - 7th Edition 2.4 ©Silberschatz, Korth and Sudarshan
Select Operation
▪ The select operation selects tuples that satisfy a given predicate.
▪ Notation: p (r)
▪ p is called the selection predicate
▪ Example: select those tuples of the instructor relation where the instructor
is in the “Physics” department.
• Query
dept_name=“Physics” (instructor)
• Result?
Database System Concepts - 7th Edition 2.5 ©Silberschatz, Korth and Sudarshan
Select Operation
▪ The select operation selects tuples that satisfy a given predicate.
▪ Notation: p (r)
▪ p is called the selection predicate
▪ Example: select those tuples of the instructor relation where the instructor
is in the “Physics” department.
• Query
dept_name=“Physics” (instructor)
• Result
Database System Concepts - 7th Edition 2.6 ©Silberschatz, Korth and Sudarshan
Practice
▪ Notation: p (r)
▪ select those tuples of the instructor relation where the name Wu
• Query?
Database System Concepts - 7th Edition 2.7 ©Silberschatz, Korth and Sudarshan
Practice
▪ Notation: p (r)
▪ select those tuples of the instructor relation where the name Wu
• Query
name=“Wu” (instructor)
• Result
Database System Concepts - 7th Edition 2.8 ©Silberschatz, Korth and Sudarshan
Practice
▪ Notation: p (r)
▪ select those tuples of the instructor relation where the name Wu
• Query
name=“Wu” (instructor)
• Result
Database System Concepts - 7th Edition 2.9 ©Silberschatz, Korth and Sudarshan
Practice
▪ select those tuples of the instructor relation where the salary is 80000
• Query?
Database System Concepts - 7th Edition 2.10 ©Silberschatz, Korth and Sudarshan
Practice
▪ select those tuples of the instructor relation where the salary is 80000
• Query
salary=“80000” (instructor)?
• salary=80000 (instructor)?
• Result
Database System Concepts - 7th Edition 2.11 ©Silberschatz, Korth and Sudarshan
Practice
▪ select those tuples of the instructor relation where the salary is 80000
• Query
salary=80000 (instructor)
• Result
Database System Concepts - 7th Edition 2.12 ©Silberschatz, Korth and Sudarshan
Select Operation (Cont.)
Database System Concepts - 7th Edition 2.13 ©Silberschatz, Korth and Sudarshan
Select Operation (Cont.)
Database System Concepts - 7th Edition 2.14 ©Silberschatz, Korth and Sudarshan
Select Operation
Query?
Database System Concepts - 7th Edition 2.15 ©Silberschatz, Korth and Sudarshan
Select Operation
Database System Concepts - 7th Edition 2.16 ©Silberschatz, Korth and Sudarshan
Select Operation (Cont.)
▪ We can combine several predicates into a larger predicate by using the
connectives:
_____________
_____________
_____________
Database System Concepts - 7th Edition 2.17 ©Silberschatz, Korth and Sudarshan
Select Operation (Cont.)
▪ We can combine several predicates into a larger predicate by using the
connectives:
(and),
(or),
(not)
▪ Example: Find the instructors in Physics with a salary greater $90,000.
Query?
Database System Concepts - 7th Edition 2.18 ©Silberschatz, Korth and Sudarshan
Select Operation (Cont.)
▪ We can combine several predicates into a larger predicate by using the
connectives:
(and),
(or),
(not)
▪ Example: Find the instructors in Physics with a salary greater $90,000,
Database System Concepts - 7th Edition 2.19 ©Silberschatz, Korth and Sudarshan
Select Operation (Cont.)
The select predicate may include comparisons between two attributes.
• Example, find all departments whose name is the same as their
building name:
• Query?
Database System Concepts - 7th Edition 2.20 ©Silberschatz, Korth and Sudarshan
Select Operation (Cont.)
The select predicate may include comparisons between two attributes.
• Example, find all departments whose name is the same as their
building name:
• Query: dept_name=building (department)
Database System Concepts - 7th Edition 2.21 ©Silberschatz, Korth and Sudarshan
Select Operation (Cont.)
▪ We allow comparisons using
=, , >, . <.
in the selection predicate.
▪ We can combine several predicates into a larger predicate by using the
connectives:
(and), (or), (not)
▪ Example: Find the instructors in Physics with a salary greater $90,000,
we write:
Database System Concepts - 7th Edition 2.22 ©Silberschatz, Korth and Sudarshan
Project Operation
Database System Concepts - 7th Edition 2.23 ©Silberschatz, Korth and Sudarshan
Project Operation
Database System Concepts - 7th Edition 2.24 ©Silberschatz, Korth and Sudarshan
Project Operation Example
Database System Concepts - 7th Edition 2.25 ©Silberschatz, Korth and Sudarshan
Project Operation Example
▪ Result ?
Database System Concepts - 7th Edition 2.26 ©Silberschatz, Korth and Sudarshan
Project Operation Example
▪ Result:
Database System Concepts - 7th Edition 2.27 ©Silberschatz, Korth and Sudarshan
Project Operation
Database System Concepts - 7th Edition 2.28 ©Silberschatz, Korth and Sudarshan
Project Operation
Produce a list of salaries for all staff, showing only staffNo, fName,
lName, and salary details.
Query?
Database System Concepts - 7th Edition 2.29 ©Silberschatz, Korth and Sudarshan
Project Operation
Produce a list of salaries for all staff, showing only staffNo, fName,
lName, and salary details.
staffNo, fName, lName, salary(Staff)
Database System Concepts - 7th Edition 2.30 ©Silberschatz, Korth and Sudarshan
Composition of Relational Operations
▪ Consider the query -- Find the names of all instructors in the Physics
department.
Query?
Database System Concepts - 7th Edition 2.31 ©Silberschatz, Korth and Sudarshan
Composition of Relational Operations
▪ Consider the query -- Find the names of all instructors in the Physics
department.
Database System Concepts - 7th Edition 2.32 ©Silberschatz, Korth and Sudarshan
Composition of Relational Operations
▪ Consider the query -- Find the names of all instructors in the Physics
department.
Query:
name( dept_name =“Physics” (instructor))
Result?
Database System Concepts - 7th Edition 2.33 ©Silberschatz, Korth and Sudarshan
Composition of Relational Operations
▪ Consider the query -- Find the names of all instructors in the Physics
department.
Query:
name( dept_name =“Physics” (instructor))
Result:
dept_name
Einstein
Gold
Database System Concepts - 7th Edition 2.34 ©Silberschatz, Korth and Sudarshan
Cartesian-Product Operation
Database System Concepts - 7th Edition 2.35 ©Silberschatz, Korth and Sudarshan
The instructor X teaches table
Database System Concepts - 7th Edition 2.36 ©Silberschatz, Korth and Sudarshan
Cartesian-Product Operation
Database System Concepts - 7th Edition 2.37 ©Silberschatz, Korth and Sudarshan
The instructor X teaches table
m * n tuples
Database System Concepts - 7th Edition 2.38 ©Silberschatz, Korth and Sudarshan
Cartesian-Product Operation
Database System Concepts - 7th Edition 2.39 ©Silberschatz, Korth and Sudarshan
Cartesian-Product Operation
• instructor.ID
• teaches.ID
Database System Concepts - 7th Edition 2.40 ©Silberschatz, Korth and Sudarshan
Join Operation
▪ The Cartesian-Product
instructor X teaches
associates every tuple of instructor with every tuple of teaches.
• Most of the resulting rows have information about instructors who did
NOT teach a particular course.
Database System Concepts - 7th Edition 2.41 ©Silberschatz, Korth and Sudarshan
Join Operation
▪ The Cartesian-Product
instructor X teaches
associates every tuple of instructor with every tuple of teaches.
• Most of the resulting rows have information about instructors who did
NOT teach a particular course.
Database System Concepts - 7th Edition 2.42 ©Silberschatz, Korth and Sudarshan
Join Operation (Cont.)
Database System Concepts - 7th Edition 2.43 ©Silberschatz, Korth and Sudarshan
Join Operation (Cont.)
▪ The join operation allows us to combine a select operation and a
Cartesian-Product operation into a single operation.
▪ Consider relations r (R) and s (S)
▪ Let “theta” be a predicate on attributes in the schema R “union” S. The
join operation r ⋈𝜃 s is defined as follows:
𝑟 ⋈𝜃 𝑠 = 𝜎𝜃 (𝑟 × 𝑠)
▪ Thus
instructor.id = teaches.id (instructor x teaches ))
Database System Concepts - 7th Edition 2.44 ©Silberschatz, Korth and Sudarshan
Join Operation (Cont.)
▪ The join operation allows us to combine a select operation and a
Cartesian-Product operation into a single operation.
▪ Consider relations r (R) and s (S)
▪ Let “theta” be a predicate on attributes in the schema R “union” S. The
join operation r ⋈𝜃 s is defined as follows:
𝑟 ⋈𝜃 𝑠 = 𝜎𝜃 (𝑟 × 𝑠)
▪ Thus
instructor.id = teaches.id (instructor x teaches ))
Database System Concepts - 7th Edition 2.45 ©Silberschatz, Korth and Sudarshan
Union Operation
Database System Concepts - 7th Edition 2.46 ©Silberschatz, Korth and Sudarshan
Union Operation
Query?
Database System Concepts - 7th Edition 2.47 ©Silberschatz, Korth and Sudarshan
Union Operation
▪ The union operation allows us to combine two relations; Notation: r s
▪ Example: In the Section relation, to find all courses (course ID) taught in
the Fall 2017 semester, or in the Spring 2018 semester, or in both
Query:
Database System Concepts - 7th Edition 2.48 ©Silberschatz, Korth and Sudarshan
Union Operation
▪ The union operation allows us to combine two relations; Notation: r s
▪ Example: In the Section relation, to find all courses taught in the Fall 2017
semester, or in the Spring 2018 semester, or in both
Result?
Database System Concepts - 7th Edition 2.49 ©Silberschatz, Korth and Sudarshan
Union Operation
▪ The union operation allows us to combine two relations; Notation: r s
▪ Example: In the Section relation, to find all courses taught in the Fall 2017
semester, or in the Spring 2018 semester, or in both
Result?
Database System Concepts - 7th Edition 2.50 ©Silberschatz, Korth and Sudarshan
Set-Intersection Operation
▪ Notation: r s
▪ Assume:
• r, s have the same _______;
• attributes of r and s are __________;
Database System Concepts - 7th Edition 2.51 ©Silberschatz, Korth and Sudarshan
Set-Intersection Operation
▪ Notation: r s
▪ Assume:
• r, s have the same arity
• attributes of r and s are compatible
Database System Concepts - 7th Edition 2.52 ©Silberschatz, Korth and Sudarshan
Set-Intersection Operation
▪ The set-intersection operation allows us to find tuples that are in both
the input relations.
▪ Notation: r s
▪ Example: Find the set of all courses taught in both the Fall 2017 and the
Spring 2018 semesters.
Query?
Database System Concepts - 7th Edition 2.53 ©Silberschatz, Korth and Sudarshan
Set-Intersection Operation
▪ The set-intersection operation allows us to find tuples that are in both
the input relations.
▪ Notation: r s
▪ Example: Find the set of all courses taught in both the Fall 2017 and the
Spring 2018 semesters.
• Result?
Database System Concepts - 7th Edition 2.54 ©Silberschatz, Korth and Sudarshan
Set-Intersection Operation
▪ The set-intersection operation allows us to find tuples that are in both
the input relations.
▪ Notation: r s
▪ Example: Find the set of all courses taught in both the Fall 2017 and the
Spring 2018 semesters.
• Result?
Database System Concepts - 7th Edition 2.55 ©Silberschatz, Korth and Sudarshan
Set Difference Operation
Database System Concepts - 7th Edition 2.56 ©Silberschatz, Korth and Sudarshan
Set Difference Operation
Database System Concepts - 7th Edition 2.57 ©Silberschatz, Korth and Sudarshan
Set Difference Operation
▪ Query?
Database System Concepts - 7th Edition 2.58 ©Silberschatz, Korth and Sudarshan
Set Difference Operation
Database System Concepts - 7th Edition 2.59 ©Silberschatz, Korth and Sudarshan
The Assignment Operation
▪ It is convenient at times to write a relational-algebra expression by
assigning parts of it to temporary relation variables.
▪ The assignment operation is denoted by and works like assignment in
a programming language.
▪ Example: Find all instructor in the “Physics” and Music department.
Physics dept_name=“Physics” (instructor)
Music dept_name=“Music” (instructor)
What is next?
Database System Concepts - 7th Edition 2.60 ©Silberschatz, Korth and Sudarshan
The Assignment Operation
▪ It is convenient at times to write a relational-algebra expression by
assigning parts of it to temporary relation variables.
▪ The assignment operation is denoted by and works like assignment in
a programming language.
▪ Example: Find all instructor in the “Physics” and Music department.
Physics dept_name=“Physics” (instructor)
Music dept_name=“Music” (instructor)
Physics Music
Database System Concepts - 7th Edition 2.61 ©Silberschatz, Korth and Sudarshan
The Rename Operation
Database System Concepts - 7th Edition 2.62 ©Silberschatz, Korth and Sudarshan
Equivalent Queries
▪ Query 2?
Database System Concepts - 7th Edition 2.63 ©Silberschatz, Korth and Sudarshan
Equivalent Queries
▪ Query 2?
Database System Concepts - 7th Edition 2.64 ©Silberschatz, Korth and Sudarshan
Equivalent Queries
▪ Example: Find information about courses taught by instructors in the
Physics department with salary greater than 90,000
▪ Query 1
dept_name=“Physics” salary > 90,000 (instructor)
▪ Query 2
dept_name=“Physics” ( salary > 90.000 (instructor))
▪ The two queries are not identical; they are, however, equivalent -- they
give the same result on any database.
Database System Concepts - 7th Edition 2.65 ©Silberschatz, Korth and Sudarshan
Relational Algebra
Database System Concepts - 7th Edition 2.66 ©Silberschatz, Korth and Sudarshan