Lecture 7
Lecture 7
• R–S
• Examples:
1. Take two relations A and B of your choice that are
union compatible and perform A – B and also B – A.
2. List the Kebeles where Students but not Instructors
of AAUCC live in Addis Ababa.
Relational Algebra
Cartesian product
• The Cartesian product operation defines a relation that is
the concatenation of every tuple of relation R with every
tuple of relation S.
• RxS
• Examples:
1. Perform this operation on two relations, say A and B,
of your choice.
2. Register each student in the department of BAIS for
every course given by the department of BAIS.
3. List the names and departments of all students who
registered for the course BAIS 322 in the second
semester of the academic year 2002.
Relational Algebra
Join Operations
Theta join (θ)
• The Theta join operation defines a relation that contains tuples
satisfying the predicate F from the Cartesian product of R and
S. The predicate F is of the form R.ai θ S.bi where θ may be
one of the comparison operators (<,≤,>,≥,=, ≠). If the
comparison operator is the = sign, then the join operation is
called Equijoin.
• R ⋈F S
• Note that: R ⋈F S = F (R x S)
• Examples:
Take your own example
Use example 3 of the previous slide.
Relational Algebra
Natural join
• The Natural join is an Equijoin of the two relations R and
S over all common attributes x. One occurrence of each
common attribute is eliminated from the result.
• R⋈S
• Examples:
Take your own example.
Use the 2nd example of the previous slide.
Relational Algebra
Outer join
• The (left) Outer join is a join in which tuples form R that do no
have matching values in the common attributes of S are also
included in the result relation. Missing values in the second
relation are set to null.
• R⋊S
• Examples:
1. Take your own example.
2. Produce a status report on registration of students.
3. Produce a status report on advisory assignment of
instructors.
4. Produce a status report on room schedules.
• Assignment:
Provide the definition for right outer join.
Relational Algebra
Semijoin
• The Semijoin operation defines a relation that contains
the tuples of R that participate in the join of R with S.
• R ⊳F S
• Examples:
Take your own example.
List complete details of all courses which are
registered by students of the department of
Accounting in the 1st semester of the academic year
2008/9.
Relational Algebra
Division Operation
Division
• Assume relation R is defined over the attribute set A and
relation S is defined over the attribute set B such that B
⊆ A. Let C = A – B. Then the Division operation R ⌯ S
is defined as a relation over the attributes C that
consists the set of tuples from R that match the
combination of every tuple in S.
• R⌯S
• Examples:
1. Take two relations R and S for which R ⌯ S can be
performed and get R ⌯ S.
2. Produce the Identification numbers of all students
who have registered for all courses given by the
department of BAIS.
Exercises
Refer to Student Records DB schema in specifying the following
queries using Relational Algebra.
1. List the names of courses on which student Abebe Bekele got “A”
grades.
2. What is the name of the instructor who advised student Abebe Bekele
in the 1st semester of the academic year 2004?
3. What grade did student Abebe Bekele got on the course Database
Systems I?
4. List the names of the courses for which student Abebe Bekele
registered in the 2nd semester of the academic year 2008.
5. List the rooms where the course Introduction to management is
scheduled in the 3rd period on Wednesdays of the current semester.
6. Show the names and departments of the instructors who are not
assigned to advise students in the current semester.
7. List the names, ranks, and date of employment of all instructors who
are qualified to teach the courses E-Commerce or Database Systems I
and II.
8. Display the course name, course code, and the name of the
department offering the course for each course that is prerequisite to
the course BAIS 411.