You are on page 1of 26

Tutorial 5

Relational Algebra

1
Basic Relational Algebra Operations

Set operations Relational DB Operations

∪ Select ()

∩ Project (P)

– Join (⨝)

Rename

(r)

others 2
SELECT Operation ()
It is used to select a subset from R that satisfies a
selection condition.
<selection condition> (R)

deg(c (R)) = deg(R),


3
Example on ()

Select all student who are in department 1 and


their ages are more than 21 or who are in
department 2 and their id smaller than 2000.

 (DNO = 1 AND age>21) OR (DNO=2 AND id < 2000 ) (EMPLOYEE)

4
PROJECT Operation (P)
It is used to select certain attributes from the
relation and discard the other attributes.
P<attribute list> (R)

deg(P<list> (R)) = the number of attributes in <list>.


the project operation removes any duplicate tuples.
Example on (P)

List name and salary for each employee.


PLNAME,FNAME,SALARY (EMPLOYEE)

6
Sequences of Operations
P FNAME,LNAME,SALARY ( DNO = 5 (EMPLOYEE))

DEPT5_EMPS ←  DNO=5 (EMPLOYEE)


RESULT ← P FNAME,LNAME,SALARY (DEPT5_EMPS)
Rename Operation

rS(B1,B2,..BN)(R)
rS(R)
r (B1,B2,..BN)(R)

Example
remp(name,age,Dnumber) (EMPLOYEE)
Set Theoretic Operations

Set Operations (Example)


(From lecture)

9
Set Operations (Example)
STUDENT FN LN
Susan Yao
Ramesh Shah FN LN
Johnny Kohler Susan Yao
Barbara Jones Ramesh Shah
Amy Ford Johnny Kohler
Jimmy Wang Barbara Jones
Emest Gilbert Amy Ford
Two union compatible relations Student ∪ Instructor Jimmy Wang
Emest Gilbert
John Smith
INSTRUCTOR FNAME LNAME
Ricardo Browne
John Smith
Francis Johnson
Ricardo Browne
Susan Yao
Francis Johnson
Ramesh Shah
CC 471 (Relational Algebra) - Dr.
Yousry Taha
(From lecture) 10
Set Operations (Example)
STUDENT FN LN
Susan Yao
Ramesh Shah
Johnny Kohler
Barbara Jones
Amy Ford
Jimmy Wang
Emest Gilbert FN LN

Two union compatible relations Student ∩ Instructor Susan Yao


Ramesh Shah

INSTRUCTOR FNAME LNAME


John Smith
Ricardo Browne
Susan Yao
Francis Johnson
Ramesh Shah
CC 471 (Relational Algebra) - Dr.
Yousry Taha
(From lecture) 11
Set Operations (Example)
STUDENT FN LN
FN LN
Susan Yao
Johnny Kohler
Ramesh Shah
Barbara Jones
Johnny Kohler
Amy Ford
Barbara Jones
STUDENT – INSTRUCTOR Jimmy Wang
Amy Ford
Emest Gilbert
Jimmy Wang
Emest Gilbert
Two union compatible relations

INSTRUCTOR FNAME LNAME FNAME LNAME


John Smith John Smith
Ricardo Browne INSTRUCTOR – STUDENT
Ricardo Browne
Susan Yao
Francis Johnson Francis Johnson
Ramesh Shah
CC 471 (Relational Algebra) - Dr.
Yousry Taha
(From lecture) 12
Set Theoretic Operations
Cartesian Product ()

student Name Dno


A 10

B 20 T Name Dno DName Dnum


C Null A 10 K 10

SD
B 20 K 10

C Null K 10

A 10 G 20
Department DName Dnum B 20 G 20
K 10 C Null G 20
G 20
Join Operation (⨝)
student Name Dno
A 10

B 20

C Null

S ⨝ <Dno=Dnumber> D
T Name Dno DName Dnumber
Department Name Dnumber A 10 K 10
K 10
B 20 G 20
G 20

14
Ex:
Retrieve manager’s name of each
department.

DEPT_MGR ← (DEPARTMENT ⨝ MGRSSN =SSN EMPLOYEE)


RESULT ← P DNAME,LNAME,FNAME (DEPT_MGR )

R ⨝C S ≡ C (R  S)
15
Natural join (*)
student Name Dno
A 10

B 20

C Null

S*D
T Name Dno Dname Dno
Department Dname Dno A 10 K 10
K 10
B 20 G 20
G 20

16
The Division Operation
Division (cont)
Operation (÷)
R A B S A
a1 b1 a1
a2 b1 a2
a3 b1 a3
a4 b1
a1 b2
T = R ÷ S
a3 b2
a2 b3
a3 b3 T B

a4 b3 b1

a1 b4 b4

a2 b4
(From lecture)
a3 b4
CC 471 (Relational Algebra) - Dr.
17
Yousry Taha
Aggregation Functions

<grouping attribute>  <function list> (R)


Ex:
DNO  COUNT SSN, AVERAGE SALARY (EMPLOYEE)

DNO COUNT_SSN AVERAGE_SALARY


5 4 33250
4 3 31000
1 1 55000

CC 471 (Relational Algebra) - Dr. Yousry Taha (From lecture) 18


Exercise 1
• Consider the following relational schema for company DB:
Emp ( Id , Name , Dno , Salary )
Dept ( Dno , DName , MId ) [“Mid” is the “Id” of the department manager]
Project ( Pno , Pname , Location , Dno )
Works-on ( Id , Pno , hours )
Dependent (Id , DepName , Birth-date )
1. Write an Algebra expression to get project name and id for all projects managed
by “Ali Fahmi”.
2. Write an Algebra expression to get employee name and id for any employee
works on all projects that employee with id “12345” works on.
3. Write an Algebra expression to retrieve employee name, employee id, employee
department name for every employee does not work in any project.
4. Write an Algebra expression to display employee id and employee name for any
employee manages more than 2 projects in “Cairo”.
5. Write an Algebra expression to retrieve the names of all employees in department
5 who work more than 10 hours per week on the “ProductX” project.
6. Write an Algebra expression to find the names of all employees who are directly
supervised by “Franklin wong”.
Answer 1
Emp ( Id , Name , Dno , Salary )
Dept ( Dno , DName , MId ) [“Mid” is the “Id” of the department manager]
Project ( Pno , Pname , Location , Dno )
Works-on ( Id , Pno , hours )
Dependent (Id , DepName , Birth-date )
1. Write an Algebra expression to get project name and id for all
projects managed by “Ali Fahmi”.

T1 ‹- πId (σ Name="Ali Fahmi"(Emp))


T2 ‹- πDno (T1 ⨝Id = Mid Dept)
Result ‹- πPno, Pname ( T2 * Project)

20
Answer 1 (con’t)
Emp ( Id , Name , Dno , Salary )
Dept ( Dno , DName , MId ) [“Mid” is the “Id” of the department manager]
Project ( Pno , Pname , Location , Dno )
Works-on ( Id , Pno , hours )
Dependent (Id , DepName , Birth-date )
2. Write an Algebra expression to get employee name and id for any
employee works on all projects that employee with id “12345”
works on.

T1 ‹- πPno (σ Id="12345" (Works_on))


T2 ‹- πId (Works_on ÷ T1)
Result ‹- πId, Name ( T2 * Emp)

21
Answer 1 (con’t)
Emp ( Id , Name , Dno , Salary )
Dept ( Dno , DName , MId ) [“Mid” is the “Id” of the department manager]
Project ( Pno , Pname , Location , Dno )
Works-on ( Id , Pno , hours )
Dependent (Id , DepName , Birth-date )
3. Write an Algebra expression to retrieve employee name,
employee id, employee department name for every employee
does not work in any project.

T1 ‹- πId (Works_on)
T2 ‹- πId (Emp)
T3 ‹- T2 – T1
Result ‹- πId, Name, DName (T3 * Emp * Dept)
Answer 1 (con’t)
Emp ( Id , Name , Dno , Salary )
Dept ( Dno , DName , MId ) [“Mid” is the “Id” of the department manager]
Project ( Pno , Pname , Location , Dno )
Works-on ( Id , Pno , hours )
Dependent (Id , DepName , Birth-date )

Write an Algebra expression to display employee id and


employee name for any employee manages Departement
that have more than 2 projects in “Cairo.
T1 ‹- σLocation = ”Cairo” (Project))
T2 ‹- ρ(Dno, no_of_projects) (Dnoτcount Pno (T1))
T3 ‹- πMid ((σno_of_projects > 2 (T2)) * Dept)
Result ‹- πId, Name (T3 ⨝Id = Mid Emp)
23
Answer 1 (con’t)
Emp ( Id , Name , Dno , Salary )
Dept ( Dno , DName , MId ) [“Mid” is the “Id” of the department manager]
Project ( Pno , Pname , Location , Dno )
Works-on ( Id , Pno , hours )
Dependent (Id , DepName , Birth-date )
4. Write an Algebra expression to display employee id and
employee name for any employee manages more than 2 projects
in “Cairo.

T1 ‹- σLocation = ”Cairo” (Project))


T2 ‹- πMid,Dno,Pno (T1 * Dept)
T3 ‹- ρ(Mid, no_of_projects) (Midτcount Pno (T2))
T4 ‹- πMid (σno_of_projects > 2 (T3))
Result ‹- πId, Name (T4 ⨝Id = Mid Emp) 24
Answer 1 (con’t)
Emp ( Id , Name , Dno , Salary )
Dept ( Dno , DName , MId ) [“Mid” is the “Id” of the department manager]
Project ( Pno , Pname , Location , Dno )
Works-on ( Id , Pno , hours )
Dependent (Id , DepName , Birth-date )
5. Write an Algebra expression to retrieve the names of all
employees in department 5 who work more than 10 hours per
week on the “ProductX” project.

T1 ‹- σPname = ”ProductX” (Project))


T2 ‹- σDno=5 (Emp))
T3 ‹- σhours > 10 (T1 * Works_on * T2))
Result ‹- πName (T3)
25
Answer 1 (con’t)
Emp ( Id , Name , Dno , Salary )
Dept ( Dno , DName , MId ) [“Mid” is the “Id” of the department manager]
Project ( Pno , Pname , Location , Dno )
Works-on ( Id , Pno , hours )
Dependent (Id , DepName , Birth-date )
6. Write an Algebra expression to find the names of all employees
who are directly supervised by “Franklin wong”.

T1 ‹- σName = ”Franklin wong” (Emp))


T2 ‹- πDno (T1 ⨝Id = Mid Dept)
Result ‹- πName (T2 * Emp)

26

You might also like