Professional Documents
Culture Documents
Relational Algebra
1
Basic Relational Algebra 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)
4
PROJECT Operation (P)
It is used to select certain attributes from the
relation and discard the other attributes.
P<attribute list> (R)
6
Sequences of Operations
P FNAME,LNAME,SALARY ( DNO = 5 (EMPLOYEE))
rS(B1,B2,..BN)(R)
rS(R)
r (B1,B2,..BN)(R)
Example
remp(name,age,Dnumber) (EMPLOYEE)
Set Theoretic Operations
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
SD
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.
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
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.
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 )
26