Chapter 4 - RA
Chapter 4 - RA
Chapter 4 - RA
Relational Algebra
• Introduction to Relational Algebra
• Set Operations: Union , Join and Difference
• Multi-table Queries: Cartesian Product , Joins , Subqueries
• Extended Operations- Extended Projection , Division
• Practice Questions
WHY RELATIONAL ALGEBRA ??
• In SQL we write WHAT we want to get form the data
• The database system needs to figure out HOW to get the data
we want
SELECT
SELECT DISTINCT
DISTINCT x.name,
x.name, z.name
z.name
FROM
FROM Product
Product x,x, Purchase
Purchase y,y, Customer
Customer zz
WHERE
WHERE x.pid
x.pid == y.pid
y.pid and
and y.cid
y.cid == z.cid
z.cid and
and
x.price
x.price >> 100
100 and
and z.city
z.city == ‘Seattle’
‘Seattle’
T1(pid,name,price,pid,cid,store) cid=cid
Temporary tables
T1, T2, . . . pid=pid
Customer
Product Purchase 6
Relational Algebra
• It is procedural query language.
• It consists of set of operations that take one or two relations as input
and produce a new relation as their result.
• The fundamental relational algebra are:
• select:
• project:
• union:
• set difference: –
• Cartesian product: x
• rename:
• Fundamental Operations
1. SELECT
• It is denoted by sigma ()
• The select operation selects tuples that satisfy the given predicate.
• The predicate appears as a subscript to .
• The augment relation is in parenthesis after as the
• dept_name = “Physics” (instructor)
• salary > 90000(instructor)
• dept_name = “Physics” ^ salary > 90000 (instructor)
2. PROJECT operation
• Suppose we want to list all instructors ID, name and salary but not
dept_name.
• The project operation allows us to produce this relation.
• The project operation is unary operation.
• It eliminates duplicates rows.
• It is denoted by pi ().
• ID,NAME,DEPT_NAME(INSTRUCTOR)
• Composition of relational operations:
• For complicated queries like “Find the name of all instructors in the physics
department”.
• name (dept_name = “Physics” (instructor))
2 α
r ∪ s: A B
1 α
1 β
2 β
2 α
• Notation: r ∪ s
• Defined as: r ∪ s = {t | t ∈ r or t ∈ s}
• 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: 2 nd column
of r deals with the same type of values as does the 2 nd column of s)
DEPOSITOR BORROWER
2 α
r - s: A B
1 α
1 β
Set Difference Operation
• Notation r – s
• Defined as: r – s = {t | t ∈ r and t ∉ s}
• 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
5. Cartesian Product Operation
Relations r : s:
rXs:
• Notation r x s
• Defined as: r x s = {t q | t ∈ r and q ∈ s}
Composition of Operations
• Can build expressions using multiple operations
Example: σA=C(r x s)
• rxs
6. Rename Operation
• Allows us to name, and therefore to refer to, the results of relational
algebra expressions.
• Allows us to refer to a relation by more than one name.
• Example:
ρ y (E)
returns the expression E under the name y
• Example:
Find the highest salary in the university.
Our Strategy
1) Compute first a temporary relation consisting of those salaries that are not
largest
2) Take the set difference between the relation Пsalary(instructor) and the
temporary relation just created.
Q) Select all employees having salary above 5000 and age above 65
Q) Find salary of all employees having age less than 25.
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower (customer_name, loan_number)
•r s:
DEPT_ID D_NAME LOCATION STUDENT_ID S_NAME DEPT_ID
C004 DIPTI D5
Relation r: s:
r÷s
Another Example of Division:
• Relation r: s:
•r÷s
Q) Select the users whose age is less than 25
Q) Select the users whose id is greater than 2 or age is not 31.
Q) Select the person who stays in Boston.
Q) Select the occupation of every person.
Generalized Projection:
• The generalized projection operation extends the projection operation by
allowing arithmetic functions to be used in the projection list.
Customer_name Limit Credit_balance
KEN 2000 1750
JONES 6000 700
SMITH 2000 400
HAYES 1500 1500
RELATIONAL SCHEMA: (a) Retrieve all information about airports in London. The
schema of the output table should be same as that of the
Airport (Airport_ID, Name, city) Airport table.
(b) Retrieve details of all bookings by British and French
Flight( flight_no, flightcompany, passengers. The schema of the output table should be
depAirport, arrairport) same as that of the Booking table.
(c) Retrieve the names of all passengers.
Booking(ticke_no, name, (d) Retrieve the flight number, Departure and Arrival
nationality, flight_no, seatno) airports of all British Airways flights.
(e) Retrieve the name of every passenger together with
Seat(seat_no, flight_no, class) their flight number and the associated flight company.
(f) Find out the ticket numbers and names of all
passengers departing from London.
Solve the following Questions using
relational algebra:
Q) Select book titles.
Q) How many authors are not book editors
Q) Write a relational algebra expression
that returns the names of all authors who
are book editors
Q) Write a relational algebra expression
that returns the names of all authors who
have at least one publication in the
database.
Q) How many tuples are returned by the
following relational algebra expression?
author author id=editor book
Solve the following Questions using relational algebra:
Q) List the year and title of each book
Q) List all information about students whose major is in CS
RELATIONAL SCHEMA:
Q) List all students with books they can borrow
BOOKS(DocId, Title, Publisher, Year) Q) List all books published by McGraw-Hill before 1990
Q) List the name of those authors who are living in Davis
STUDENTS(StId, StName, Major, Age) Q) List the name of students who are older than 30 and
AUTHORS(AName, Address) who are not studying CS
Q) Rename AName in the relation AUTHORS to Name
borrows(DocId, StId, Date) Q) List the names of all students who have borrowed a book
and who are CS majors.
has-written(DocId, AName) Q) List each book with its keywords.
describes(DocId, Keyword) Q) List the authors of the books the student ’Smith’ has
borrowed.
Q) Which books have both keywords ’database’ and
’programming’?
Solve the following Questions using relational algebra:
• Retrieve details of all films that were released in 2010. The
output schema should be same as that of the Film table.
SCHEMA: • Retrieve details of all actors that are not in their thirties. The
output schema should be the same as that of actor table.
• Actor (actorId, name, • Retrieve the names of all directors.
nationality, age) • Retrieve the names of all American directors.
• Film (filmId, title, year, • Find out the names of all British actors above the age of 40.
directorId) • Retrieve the name of each actor together with the titles of
the film he/she has performed in.
• Performance (actorId, • Find out the names of all actors that have played the
filmId, character) character of Bruce Wayne, together with the year
corresponding films were released.
• Director (directorId, • Retrieve all actors from the film inception.
name, nationality)
Tuple Relational Calculus
Tuple Relational Calculus
Find the set of all courses taught in the Fall 2009 semester, or in
the Spring 2010 semester, or both
Find the set of all courses taught in the Fall 2009 semester, but not in
the Spring 2010 semester
{ x , x , …, x | P (x , x , …, x )}
1 2 n 1 2 n