# relational-algebra-1

```CPSC471 Database Management Systems
NOTES on Relational Algebra
N.B. : Duplicate tuples are eliminated by the project ( P ) operation.
CARTESIAN PRODUCT ( X ) vs JOIN ( X )
Given two relations: R1(A1, A2, ..., Am) and R2 (B1, B2, ..., Bn)
R1 X R2 returns a new relation say R(A1, A2, ..., Am, B1, B2, ..., Bn)
Example:
Student
Department
Sid
20
35
48
51
59
60
Name
Jim
Susan
Diana
John
Lara
David
dept
CS
EE
CS
CS
EE
EE
Student X Department
Sid
Name
20
Jim
35
Susan
48
Diana
51
John
59
Lara
60
David
dept
CS
EE
CS
CS
EE
EE
Dname
CS
CS
CS
CS
CS
CS
Eddy
Eddy
Eddy
Eddy
Eddy
Eddy
location
CSB
CSB
CSB
CSB
CSB
CSB
CS
EE
CS
CS
EE
EE
EE
EE
EE
EE
EE
EE
EEB
EEB
EEB
EEB
EEB
EEB
20
35
48
51
59
60
s---
(dept=dname)
Jim
Susan
Diana
John
Lara
David
CS
Eddy CSB
EE
(Student X Department) Student
Sid
20
48
51
35
59
60
Name
Jim
Diana
John
Susan
Lara
David
dept
CS
CS
CS
EE
EE
EE
&quot;! (dept=dname) Department
Dname
(dept=department)
CS
Eddy CSB
CS
Eddy CSB
CS
Eddy CSB
EE
EE
EE
So, JOIN is equivalent to CROSS-PRODUCT followed by SELECT.
CPSC471 Database Management Systems
Division: ( &divide;) (done using a combination of -, X, P, s-- )
Example:
Project
Pno
20
20
30
35
35
35
Part
P#
2
4
1
1
2
4
P#
1
2
4
weight
150
130
95
Color
red
green
blue
Query: Find projects using all parts.
Project &divide; (PP# Part )
Pno
35
How does it work:
1. Find all Projects and all Parts
PPno Project
PP# Part
Pno
P#
20
1
30
2
35
4
2. Find what should have been the relationship between projects and parts if each project is
using every part. This is the cross-product of the results in 1. That is, assume every
project is using all the parts.
(PPno Project ) X (PP# Part)
Pno
20
20
20
30
30
30
35
35
35
P#
1
2
4
1
2
4
1
2
4
CPSC471 Database Management Systems
3. Find tuples in 2, which are not part of the result; that is, we need to find from 2 tuples that
have been assumed but they are not reflecting the real case. These are projects that do not
use all parts.
((PPno Project ) X (PP# Part)) - Project
Pno
20
30
30
P#
1
2
4
4. Subtract projects in 3 from the existing projects to get projects using all the parts.
(PPno Project) - (PPno(((PPno Project ) X (PP# Part)) - Project)
Pno
20
30
35
Pno
20
30
=
Result
Pno
35
Aggregate Functions: ( &Aacute;) : to execute a certain aggregate function on tuples of a relation
after the tuples are categorized (classified) by the values of certain attributes.
Group-by attributes &Aacute; function list (relation)
Example:
Student
Sid
20
35
48
51
59
60
Name
Jim
Susan
Diana
John
Lara
David
dept
CS
EE
CS
CS
EE
EE
Query: Find the number of students in each department
dept &Aacute; count sid (Student)
dept
count
CS
3
EE
3
```