Relational Databases: Relational Algebra (1) Select, Project, Join
Relational Databases: Relational Algebra (1) Select, Project, Join
Relational Databases: Relational Algebra (1) Select, Project, Join
In R1 x R2,
R1 and R2 are the operands while x is the operator . The operator is applied
on operands and new value is constructed.
What is Relational Algebra?
An algebra whose operands are relations. Operators are designed to do the
most common things that we need to do with relations in a database.
This results in an algebra that can be used as a query language for relations.
Student
we'll get as a result a copy of the student relation
1 Comsats CS No
1 Comsats EE Yes
2 NUST CS No
1 CS
1 EE
2 CS
Relational Algebra (1)
To pick both rows and columns…
Student
sID sName GPA HS
1 Ahmed 3.4 1200
1 Comsats CS No
1 Comsats EE Yes
2 NUST CS No
CS No
EE Yes
The semantics of relational algebra says that duplicates are always eliminated. So if you
run a query that would logically have a lot of duplicate values, you just get one value for
each result.
Relational Algebra (1)
Cross-product (X): combine two relations
(a.k.a. Cartesian product)
Student Apply
sID uName major dec
sID sName GPA HS
1 Comsats CS No
1 Ahmed 3.4 1200
1` Comsats EE Yes
2 Ali 3.75 2000 X 2 NUST CS No
=
Student.sID sName GPA HS Apply.sID uName major dec
Student Apply
sID sName GPA HS uName major dec
sID uName major dec
sID sName GPA HS
1 Ahmed 3.4 1200 1 Comsats CS No
⋈ = 1 Ahmed 3.4 1200 Comsats CS No
1` Comsats EE Yes
2 Ali 3.75 2000 1 Ahmed 3.4 1200 Comsats EE Yes
2 NUST CS No 2 Ali 3.75 2000 NUST CS No
Relational Algebra (1)
Natural Join (⋈)
Names
NamesandandGPAs
GPAsof ofstudents
studentswithwithHS>1000
HS>1000who whoapplied
appliedto
toCS
CSand
were rejected
and were rejected
∏sName,GPA ( σ HS>1000 ^ major=‘CS’ ^ dec=‘No’ (Students ⋈ Apply ) )