You are on page 1of 10

ClassTutorial 1

Relational Algebra

EX 1. Result of mapping the COMPANY ER schema into a relational schema.

QUERIES to COMPANY Schema


Retrieve the FN, LN, Salary of all the employees who works in

Dept. No. 5

Retrieve the SSN of all the employees who works in Dept No. 4

Retrieve the SSN of all employees who either works in Dept No.

5 or directly supervise an employee who works in Dept No. 5

Retrieve the names of the manager of each department.

QUERIES : EXAMPLE 2
Assume the following relational schemas:
employee (person_name, street, city) company (company_name, city) works (person_name, company_name, salary) manages (person_name, manager_name)

Basic Relational Algebra Operations


Select:
Find all companies located in Hong Kong

city = Hong kong(company)

Project:
Find the names of all persons that have a job person_name(works)

Union:
Find the name of every person who is an employee or a manager person_name(employee) manager_name(manages)
There is automatic duplicate elimination in all algebra operations.

Basic Relational Algebra Operations (cont)


Set difference: Find the names of all persons who are employees but
not managers person_name(employee) manager_name(manages)

Cartesian product: Produce all possible combinations of employees and


companies employee company

Rename: Rename the table employee to worker worker(employee)

Additional Relational Algebra Operations


Set intersection:
Find the names of all persons who are both employees
and managers

person_name(employee) manager_name(manages)

Exercise
Assume the following relational schemas:

employee (person_name, street, city) company (company_name, city) works (person_name, company_name, salary) manages (person_name, manager_name)

Give an expression in relational algebra to express each of the

following queries:
Find the names of all employees who work for First Bank Corporation Find the names of employees who do not work for First Bank Corporation

Query 1
Find the names of all employees who work for First

Bank Corporation
person_name(company_name = First Bank Corporation(works))

Query 2
Find the names of employees who do not work for

First Bank Corporation


person_name(employee)

person_name(company_name = First Bank Corporation(works))

You might also like