Download as pdf or txt
Download as pdf or txt
You are on page 1of 122

Relational model

and
Relational Algebra
(part-2)
Chapter 3: Relational model and Relational
Algebra (T2: Page 173-200)

• Unary Relational Operations: Select and Project


• Relational Algebra Operations from Set Theory
• Binary Relational Operations: JOIN and DIVISION
• Additional Relational Operations
• Examples of Queries in Relational Algebra

B.S.Shankar, Dept of MCA, VVIET 2


Relational Algebra
• A data model must include a set of operation to manipulate
the database.
• This basic set of operation for relational model is Relational
Algebra.
• A sequence of relational algebra operations forms a Relational
Algebra expression.
• It provides a formal foundation for relational model operation.
• It is used as a basis for implementing and optimizing queries in
RDBMS.
• Some of its concepts are incorporated into the SQL (standard
query language) for RDBMS.

B.S.Shankar, Dept of MCA, VVIET 3


Relational Algebra

• These algebraic operations enable a user to specify basic


retrieval requests.

• The result of a retrieval is a new relation, which may be


formed from one or more relations.

B.S.Shankar, Dept of MCA, VVIET 4


Relational Algebra

• Unary Relational Operations:


– These operations operate on single relations.

• Binary Relational Operations:


– These operations operate on two relations.

B.S.Shankar, Dept of MCA, VVIET 5


Relational Algebra

Two groups of operations


• Set Operations based on mathematical set theory
– Union
– Intersection
– Difference
– Cartesian product
• Operations developed specially for relational database:
– Select -- Unary
– Project -- Unary
– Join -- Binary

B.S.Shankar, Dept of MCA, VVIET 6


Select operation

• Select operation is used to select a subset of the tuples


from a relation that satisfy a selection condition.
• It is a horizontal partitioning of the relation:
– One partition satisfying the condition will be shown as
result.
– Other partition not satisfying the condition will be
discarded.
• SELECT operation is unary

B.S.Shankar, Dept of MCA, VVIET 7


Select operation

• σ <selection condition> (R)


– Where σ (sigma) is for select operation
– selection condition is a boolean expression specified
on the attributes of Relation R

• Clauses of selection condition


– <Attribute name> <comparison operator> <constant
value>
– <Attribute name> <comparison operator> <Attribute
name>
B.S.Shankar, Dept of MCA, VVIET 8
Select operation

Empid Name Salary


Employee
101 Ram 20,000
102 Shyam 22,000
σ salary>20,000 (Employee)
103 Geeta 24,000
104 Geeta 25,000
105 Pam 18,000

Resultant table: ?

B.S.Shankar, Dept of MCA, VVIET 9


Select operation

Empid Name Salary


Employee
101 Ram 20,000
102 Shyam 22,000
103 Geeta 24,000
104 Geeta 25,000
σ salary>20,000 (Employee) 105 Pam 18,000

Resultant Empid Name Salary


table: 102 Shyam 22,000
Employee 103 Geeta 24,000
104 Geeta 25,000
B.S.Shankar, Dept of MCA, VVIET 10
Select operation

• Comparison operator
• =, <, >, ≠, ≥,≤ for numeric value or date
• =, ≠ for strings of characters

• Constant value
– Value from attribute domain

B.S.Shankar, Dept of MCA, VVIET 11


Select operation

• Degree of resulting relation is same as that of R


• No. of tuples in resulting relation <= no. of tuples in R
– i.e. |σc (R)| <= |R|
• Selection condition may be more than one
– i.e. (cond1 and cond2)  true if both are true
– i.e. (cond1 or cond2)  true if any one or both are
true
– NOT cond true if cond is false

B.S.Shankar, Dept of MCA, VVIET 12


Select operation: expression

Empid Name Dno Salary


Employee
101 Ram 1 20,000
102 Shyam 2 22,000
103 Geeta 3 24,000
104 Geeta 2 25,000
105 Pam 2 18,000

σ (dno =3 and salary>20000) or (dno=2 and salary< 20000 (Employee))

Resultant table: ?

B.S.Shankar, Dept of MCA, VVIET 13


Select operation: expression

Empid Name Dno Salary


Employee
101 Ram 1 20,000
102 Shyam 2 22,000
103 Geeta 3 24,000
104 Geeta 2 25,000
105 Pam 2 18,000
σ (dno =3 and salary>20000) or (dno=2 and salary< 20000 (Employee))
Empid Name Dno Salary
Resultant table:
103 Geeta 3 24,000
Employee 105 Pam 2 18,000
B.S.Shankar, Dept of MCA, VVIET 14
Select operation

• Select operation is Commutative:


– σ<cond1> (σ<cond2> (R)) = σ<cond2> (σ<cond1> (R))
– A sequence of Selects can be applied in any order.

• Cascade of Selects can be used through one select using


Conjunctive (AND)
– σ<cond1> (σ<cond2> (….(σ<condn>(R))……))
= σ<cond1> AND<cond2>AND…… AND<condn> (R)

B.S.Shankar, Dept of MCA, VVIET 15


Select operation: Commutative

Employee Empid Name Dno Salary


101 Ram 1 20,000
102 Shyam 2 22,000
103 Geeta 3 24,000
104 Geeta 2 25,000
105 Pam 2 18,000

σ salary>20,000 (σ Dno=2 (Employee))


Resultant table: ?

B.S.Shankar, Dept of MCA, VVIET 16


Select operation: Commutative

Empid Name Dno Salary


Employee
101 Ram 1 20,000
102 Shyam 2 22,000
103 Geeta 3 24,000
104 Geeta 2 25,000
105 Pam 2 18,000
σ salary>20,000 (σ Dno=2 (Employee))
Empid Name Dno Salary
Resultant table:
102 Shyam 2 22,000
Employee 104 Geeta 2 25,000
B.S.Shankar, Dept of MCA, VVIET 17
Select operation: Commutative

Empid Name Dno Salary


Employee
101 Ram 1 20,000
102 Shyam 2 22,000
103 Geeta 3 24,000
104 Geeta 2 25,000
105 Pam 2 18,000

σ Dno=2 (σ salary>20,000 (Employee))

Resultant table: ?

B.S.Shankar, Dept of MCA, VVIET 18


Select operation: Commutative
Empid Name Dno Salary
Employee
101 Ram 1 20,000
102 Shyam 2 22,000
103 Geeta 3 24,000
104 Geeta 2 25,000
105 Pam 2 18,000
σ Dno=2 (σ salary>20,000 (Employee))
Empid Name Dno Salary
Resultant table:
102 Shyam 2 22,000
Employee
104 Geeta 2 25,000
B.S.Shankar, Dept of MCA, VVIET 19
Project Operation

• Project operation selects certain columns from relation


and discards rest.

• It is a vertical partitioning of the relation:


– One partition satisfying the condition will be shown as
result.
– Other partition not satisfying the condition and will be
discarded.

• PROJECT operation is unary

B.S.Shankar, Dept of MCA, VVIET 20


Project Operation

• Π <attribute list> (R)


– Where Π (pi) is for project operation

• Attribute list is the desired list of attributes from relation


R

• Resulting relation has attribute only those specified in list


and in same order

B.S.Shankar, Dept of MCA, VVIET 21


Project Operation
Empid Name Salary
Employee 101 Ram 20,000
102 Shyam 22,000
103 Geeta 24,000
104 Geeta 25,000

Π empid, salary (Employee)

Resultant table: ?

B.S.Shankar, Dept of MCA, VVIET 22


Project Operation
Empid Name Salary
Employee 101 Ram 20,000
102 Shyam 22,000
103 Geeta 24,000
104 Geeta 25,000
Π empid, salary (Employee)
Empid Salary
Resultant 101 20,000
table: 102 22,000
Employee 103 24,000
104 25,000
B.S.Shankar, Dept of MCA, VVIET 23
Project Operation

Empid Name Salary


Employee
101 Ram 20,000
102 Shyam 22,000
103 Geeta 25,000
104 Geeta 25,000

Π name, salary (Employee)

Resultant table: ?

B.S.Shankar, Dept of MCA, VVIET 24


Project Operation

Empid Name Salary


Employee
101 Ram 20,000
102 Shyam 22,000
103 Geeta 25,000
104 Geeta 25,000
Π name, salary (Employee)
Name Salary
Resultant
Ram 20,000
table:
Shyam 22,000
Employee
Geeta 25,000
B.S.Shankar, Dept of MCA, VVIET 25
Project Operation

• Degree of resulting relation is same as no. of attributes in


list
• No. of tuples in resulting relation?
– If attribute list has Primary key, the no. of tuples will be
same as in Relation R
– If attribute list does not have Primary key, the duplicate
tuples will be removed to give valid relation
– Thus, no. of tuples in resulting relation <= no. of tuples
in relation R

B.S.Shankar, Dept of MCA, VVIET 26


Project Operation

• Project operation is not Commutative:

– Π<list1> (Π<list2> (R)) ≠ Π<list2> (Π<list1> (R))

• Following is true if list1 is subset of list2, otherwise false


– Π<list1> (Π<list2> (R)) = Π<list1> (R)

B.S.Shankar, Dept of MCA, VVIET 27


Project Operation: not Commutative
Empid Name Salary

Employee 101 Ram 20,000


102 Shyam 22,000
103 Geeta 24,000
104 Geeta 25,000

Π empid ( Π empid, salary (Employee))

Resultant table: ?

Π salary ( Π empid, salary (Employee))

Resultant table: ?

B.S.Shankar, Dept of MCA, VVIET 28


Project Operation: not Commutative
Empid Name Salary
101 Ram 20,000
Employee
102 Shyam 22,000
103 Geeta 24,000
104 Geeta 25,000
Π empid ( Π empid, salary (Employee)) Salary
Empid 20,000
Resultant
101 table: 22,000
Resultant
table: 102 Employee 24,000
Employee 103 25,000
104 Π salary ( Π empid, salary (Employee))
B.S.Shankar, Dept of MCA, VVIET 29
Project Operation: not Commutative

Employee Empid Name Salary


101 Ram 20,000
102 Shyam 22,000
103 Geeta 24,000
104 Geeta 25,000

Π empid ( Π empid, salary((Employee))


Resultant table: ?

Π empid (Employee))
Resultant table: ?
B.S.Shankar, Dept of MCA, VVIET 30
Project Operation: not Commutative

Employee Empid Name Salary


101 Ram 20,000

Π empid ( Π empid, salary((Employee))


102 Shyam 22,000
103 Geeta 24,000
Empid 104 Geeta 25,000
Resultant
table: 101

Employee
102 Empid
103 Π empid (Employee))
101
104 Employee  102
103
104
B.S.Shankar, Dept of MCA, VVIET 31
Sequence of operations Operation

• Several algebraic expressions:


– As a single relational expression by nesting operations
– Π<list1> (σ<cond1> (R))
Or
– Name the intermediate relation and get final result as a
series of operations
– temp  σ<cond1> (R)
– result  Π<list1> (temp)

B.S.Shankar, Dept of MCA, VVIET 32


Sequence of operations Operation

Empid Name Salary


Employee 101 Ram 20,000
102 Shyam 22,000
103 Geeta 24,000
104 Geeta 25,000

Π empid (σ salary>20,000 (Employee))

Resultant table: ?

B.S.Shankar, Dept of MCA, VVIET 33


Sequence of operations Operation

Empid Name Salary


Employee 101 Ram 20,000
102 Shyam 22,000
103 Geeta 24,000
104 Geeta 25,000

Π empid (σ salary>20,000 (Employee)) Empid


102
Resultant table: Employee 103
104
B.S.Shankar, Dept of MCA, VVIET 34
Sequence of operations Operation

temp  σ salary>20,000 (Employee)

Empid Name Salary


102 Shyam 22,000
103 Geeta 24,000
104 Geeta 25,000

Resultant Table = Π empid (temp) Empid


102
103
104
B.S.Shankar, Dept of MCA, VVIET 35
Rename

• Attributes can be renamed in resulting relation


– temp  σ<cond1> (Employee)
– Emp (empid, empname, empsalary)  Π<eid, ename, salary>
(temp)

• If no renaming is applied, resulting relation will have same


names and order of attributes as parent relation has.

B.S.Shankar, Dept of MCA, VVIET 36


Sequence of operations Operation: Rename
resulting relation

Employee Empid Name Salary


101 Ram 20,000
102 Shyam 22,000
103 Geeta 24,000
104 Geeta 25,000
EMP
temp  σ salary>20,000 (Employee) EID
EMP(EID)  Π empid (temp) 102
103
104
B.S.Shankar, Dept of MCA, VVIET 37
Formal Rename operation

• Renaming both relation name and attributes


ρS(B1, B2,….Bn) (R)

• Renaming only relation name


ρS (R)

• Renaming only attributes


ρ(B1, B2,….Bn) (R)

B.S.Shankar, Dept of MCA, VVIET 38


Formal Rename operation (ex)

• Renaming both relation name and attributes


ρEMPLOYEE(empno, empname,birthdate) (emp)

• Renaming only relation name


ρEMPLOYEE (emp)

• Renaming only attributes


ρ(empno, empname,birthdate) (emp)

B.S.Shankar, Dept of MCA, VVIET 39


Set Theoretic Operation

• Binary operation
• Both relational should be union compatible
• Union compatible has following characteristics:
– i.e. for R(A1, A2, ….An) & S(B1,B2,….Bn)
– dom(Ai) = dom(Bi) for 1 <= i <=n
– Degree of both relations should be same.
• UNION (RUS)
• INTERSECTION (R ∩S)
• MINUS (R-S)

B.S.Shankar, Dept of MCA, VVIET 40


Set Theoretic Operation

• UNION (RUS)
– It includes all tuples that are either in R or S or in both
– Duplicate tuples are eliminated.

B.S.Shankar, Dept of MCA, VVIET 41


UNION
Student
Id Name
101 Ram
102 Shyam
103 Geeta
104 Rita Student U Instructor = ?

Instructor
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 42
UNION
Student
Id Name Student U Instructor
101 Ram
Id Name
102 Shyam
103 Geeta 101 Ram
104 Rita 102 Shyam
103 Geeta
Instructor 104 Rita
Id Name 107 Smith
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 43
Set Theoretic Operation

• INTERSECTION (R∩S)
– It includes all tuples that are in R & S both

B.S.Shankar, Dept of MCA, VVIET 44


INTERSECTION
Student
Id Name
101 Ram
102 Shyam
103 Geeta
Student ∩ Instructor = ?
104 Rita

Instructor
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 45
INTERSECTION
Student
Id Name
101 Ram Student ∩ Instructor
102 Shyam
Id Name
103 Geeta
104 Rita 102 Shyam
103 Geeta
Instructor
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 46
Set Theoretic Operation

• Set difference (MINUS)

• (R-S)
– It includes all tuples that are in R but not in S

• (S-R)
– It includes all tuples that are in S but not in R

• (R-S) ≠ (S-R)

B.S.Shankar, Dept of MCA, VVIET 47


DIFFERENCE (R-S)
Student
Id Name
101 Ram
102 Shyam
103 Geeta
104 Rita
Student – Instructor = ?
Instructor
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 48
DIFFERENCE (R-S)
Student
Id Name
101 Ram
102 Shyam
103 Geeta
Student - Instructor
104 Rita Id Name
101 Ram
Instructor
104 Rita
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 49
DIFFERENCE (S-R)
Student

Id Name
101 Ram
102 Shyam
103 Geeta
104 Rita
Instructor – Student = ?
Instructor
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 50
DIFFERENCE (S-R)
Student
Id Name
101 Ram
102 Shyam
Instructor - Student
103 Geeta
104 Rita Id Name
107 Smith
Instructor
Id Name
102 Shyam
103 Geeta
107 Smith
B.S.Shankar, Dept of MCA, VVIET 51
DIFFERENCE: not commutative

Student - Instructor Instructor - Student


Id Name Id Name
101 Ram
107 Smith
104 Rita

B.S.Shankar, Dept of MCA, VVIET 52


Set Theoretic Operation

• Union & Intersection are:


1. Commutative
• (R U S) = (S U R)
• (R ∩ S) = (S ∩ R)

• Associative
• (R U (S U T) = (R U S) U T)
• (R ∩ (S ∩ T) = (R ∩ S) ∩ T)

B.S.Shankar, Dept of MCA, VVIET 53


UNION (Commutative)
Student Instructor
Id Name Id Name
101 Ram 102 Shyam
102 Shyam
103 Geeta
103 Geeta
107 Smith
104 Rita

Student U Instructor Instructor U Student


Id Name Id Name
101 Ram 101 Ram
102 Shyam 102 Shyam
103 Geeta 103 Geeta
104 Rita 104 Rita
107 Smith 107 Smith
B.S.Shankar, Dept of MCA, VVIET 54
INTERSECTION (Commutative)
Student
Instructor
Id Name
101 Ram Id Name
102 Shyam 102 Shyam
103 Geeta 103 Geeta
104 Rita 107 Smith

Student ∩ Instructor Instructor ∩ Student


Id Name Id Name
102 Shyam 102 Shyam
103 Geeta 103 Geeta

B.S.Shankar, Dept of MCA, VVIET 55


Associative
Student Player
Id Name Id Name
101 Ram 103 Geeta
102 Shyam 102 Shyam
103 Geeta
(Student U Instructor) U Player
Instructor
Id Name
Id Name 101 Ram
102 Shyam 103 Geeta
104 Smith 104 Rita
105 Nita 105 John
102 Shyam
B.S.Shankar, Dept of MCA, VVIET 56
Associative
Student Player
Id Name
Id Name
103 Geeta
101 Ram
102 Shyam
102 Shyam
103 Geeta Student U (Instructor U Player )
Id Name
Instructor
101 Ram
Id Name 103 Geeta
102 Shyam
104 Rita
104 Smith
105 John
105 Nita
102 Shyam
B.S.Shankar, Dept of MCA, VVIET 57
Cartesian Product

• Also known as cross product or cross join


• Binary Operation
• Union Compatibility is not required
• RхS

B.S.Shankar, Dept of MCA, VVIET 58


Cartesian Product

Employee Dependant
Emp_id Name Dep_name Bdate
101 Ram
Meena 23-02-1988
102 Shyam
Raju 23-02-1990
103 Geeta

Employee х dependant = ?

B.S.Shankar, Dept of MCA, VVIET 59


Cartesian Product

employee х dependant

Emp_id Name Dep_name Bdate


101 Ram Meena 23-02-1988
101 Ram Raju 23-02-1990
102 Shyam Meena 23-02-1990
102 Shyam Raju 23-02-1990
103 Geeta Meena 23-02-1990
103 Geeta Raju 23-02-1990

B.S.Shankar, Dept of MCA, VVIET 60


Cartesian Product

• no. of tuples in Resultant relation = no. of tuples in R х no.


of tuples in S
• It includes one tuple from each combination
• No. of attributes in Resultant relation = n + m
• R(A1, A2,…,An) х S(B1, B2,...,Bn) = Q(A1, A2,…,An, B1,
B2,….,Bn)
• Result will have some irrelevant tuple which can be
further processed by select & project operation

B.S.Shankar, Dept of MCA, VVIET 61


Binary relational Operations

• JOIN
• DIVISION

B.S.Shankar, Dept of MCA, VVIET 62


JOIN Operation

• R S It is used to combine related tuples from two


relations into single tuples.
• It allows to process relationships among relations
• R <join condition> S
– Attributes in Resultant relation = n + m
– R(A1, A2,…..,An) S(B1, B2,….,Bn) = Q(A1,
A2,…..,An, B1, B2,….,Bn)
– No. of tuples wherever join condition is satisfied.

B.S.Shankar, Dept of MCA, VVIET 63


JOIN Operation and Cartesian product

• In Join, combinations of tuples satisfying joining


condition appear in the result.

• Whereas in Cartesian product, all combinations of tuples


are included in the result.

• Join operation can be stated as Cartesian product


operation followed by Select operation.

B.S.Shankar, Dept of MCA, VVIET 64


JOIN Operation
• A general JOIN condition is of the form
– <condition> AND <condition> AND ….AND <condition>
– Each condition is of the form Ai θ Bi
• Theta join:
– Condition is of the form Ai θ Bi where θ is one of the
{=,<,>,≥,≤,≠}
– dom(Ai) = dom(Bi)
– Tuples whose join attributes are null do not appear in
the result
– S1 S1.sid < R1.sid R1

B.S.Shankar, Dept of MCA, VVIET 65


JOIN Operation

• EQUI JOIN
– When comparison Operator used is =, is called an
EQUI JOIN

• One or more pairs of attributes that have identical values

B.S.Shankar, Dept of MCA, VVIET 66


EQUI Join operation

Employee
emp_id Name
Employee emp_id=eid Dependant
101 Ram
emp_id eid Name Dep_name
102 Shyam
101 101 Ram Meena
103 Geeta
102 102 Shyam Raju
Dependant
Dep_name eid
Meena 101
Raju 102
B.S.Shankar, Dept of MCA, VVIET 67
JOIN Operation

• In EQUI JOIN one pair of attribute have identical values in


each tuple i.e. one attribute is superfluous, a new JOIN
can be applied:
– Natural JOIN i.e. R * S

• Both the tables should have one same attribute with


same name as joining attribute

• If attribute is same but name is different (first perform


rename than natural join)
B.S.Shankar, Dept of MCA, VVIET 68
Join operation: Natural JOIN

Employee Employee * Dependant


emp_id Name emp_id Name Dep_name
101 Ram
101 Ram Meena
102 Shyam
102 Shyam Raju
103 Geeta

Dependant
Dep_name emp_id

Meena 101
Raju 102
B.S.Shankar, Dept of MCA, VVIET 69
JOIN Operation

• If join condition is not satisfied  no tuple in resultant


relation
• If no. of tuples in R = nR
• If no. of tuples in S = nS
• Tuples in resultant of EQUI JOIN are in between zero and
nR * nS
• Join selectivity ratio = expected size of join result /
maximum size

B.S.Shankar, Dept of MCA, VVIET 70


m-way JOIN Operation

((Project DNUM = DNUMBER Department) MGRSSN = SSN Employee)

B.S.Shankar, Dept of MCA, VVIET 71


A complete set of relational algebra operation

• {σ, п, U, -, х) is a complete set


• R ∩ S = (RUS) – ((R-S) U (S-R))
• R <condition> S = σ <condition> (R х S)
• Natural join = п…(σ…(ρ….(R х S)))
• But for convenience, different operations are used.

B.S.Shankar, Dept of MCA, VVIET 72


Join operation
Employee Dependant
SSN Name Dep_name ESSN
101 Ram
Meena 101
102 Shyam
Raju 102
103 Geeta

Employee X Dependant = ?

σSSN = ESSN (Employee X Dependant) = ?

B.S.Shankar, Dept of MCA, VVIET 73


Join operation
Employee X Dependant
SSN Name ESSN Dep_name
101 Ram 101 Meena
101 Ram 102 Raju
102 Shyam 101 Meena
102 Shyam 102 Raju
103 Geeta 101 Meena
103 Geeta 102 Raju

B.S.Shankar, Dept of MCA, VVIET 74


Join operation

σSSN = ESSN (Employee X Dependant)

SSN Name ESSN Dep_name


101 Ram 101 Meena
102 Shyam 102 Raju

B.S.Shankar, Dept of MCA, VVIET 75


Division operation

• Division operation is suited to queries that include the


phrase “for all”
• Denoted by ÷
• Operation is applied to two relations
R(Z) ÷ S(X)

• Division operation is applied to two relations


R(Z) ÷ S(X) where X is subset of Z and attribute Y of R that
is not attribute of S i.e. (Z-X) = Y

B.S.Shankar, Dept of MCA, VVIET 76


Division

R S R÷S
ESSN PNO PNO
12345 1
1
12345 2
2
23456 2
23456 1
35346 1
21336 2
B.S.Shankar, Dept of MCA, VVIET 77
Division

R S R÷S
ESSN PNO PNO ESSN
12345 1 12345
1
12345 2
2 23456
23456 2
23456 1
35346 1
21336 2
B.S.Shankar, Dept of MCA, VVIET 78
Notation for Query Trees

• Query tree is also known as Query Evaluation tree or


Query Execution tree.

• It includes the relational algebra operations being


executed and is used as a possible data structure for the
internal representation for the query in an RDBMS.

• A query tree is a data structure that corresponds to a


relational algebra expression.

B.S.Shankar, Dept of MCA, VVIET 79


Notation for Query Trees

• Query tree represents the input relations of the query as


leaf nodes of the tree, and represents the relational
algebra operations as internal nodes.

• An execution of the query tree consists of executing an


internal node operation whenever its operands are
available.

• Then internal node is replaced by the relation that results


from executing the operation.

B.S.Shankar, Dept of MCA, VVIET 80


Notation for Query Trees (Example)

• For every project located in ‘Stafford’, list the


project number, the controlling department
number, and the department manager’s last name,
address and birth date.

B.S.Shankar, Dept of MCA, VVIET 81


B.S.Shankar, Dept of MCA, VVIET 82
Notation for Query Trees (Example)

• Temp1  σPlocation=‘Stafford’ (PROJECT)


• Temp2  Temp1 ⋈ Dnum=Dnumber (DEPARTMENT)
• Temp3  Temp2 ⋈ Mgr_SSN=SSN (EMPLOYEE)

• ΠPnumber,Dnum,Lname,Bdate(Temp3)

B.S.Shankar, Dept of MCA, VVIET 83


Notation for Query Trees (Example)

• ΠPnumber,Dnum,Lname,Bdate(((σPlocation=‘Stafford’(PROJ
ECT))
⋈Dnum=Dnumber(DEPARTMENT))
⋈Mgr_SSN=SSN(EMPLOYEE))

B.S.Shankar, Dept of MCA, VVIET 84


Notation for Query Trees (Example)

Π P.Pnumber, P.Dnum, E.Lname, E.Bdate


(3)
⋈ D.Mgr_SSN=E.SSN

(2) EMPLOYEE
⋈ E
P.Dnum=D.Dnumber

(1) D DEPARTMENT
σP.Plocation=‘Stafford’

P PROJECT
B.S.Shankar, Dept of MCA, VVIET 85
Additional Relational Operations

B.S.Shankar, Dept of MCA, VVIET 86


Generalized Projection

• Generalized projection operation extends the projection


operation by allowing functions of attributes to be
included in the projection list.
• ΠF1, F2,….,Fn (R)
Where F1, F2,….,Fn are functions over the attributes in the
relation R and may involve constants.

B.S.Shankar, Dept of MCA, VVIET 87


Generalized Projection (Example)

Relation Given
• EMPLOYEE (Ssn, Salary, Deduction, Years_service)

Result Expected
Report (Ssn, Net_salary, Bonus, Tax)

• REPORT ρ(Ssn, Net_salary, Bonus, Tax)


(ΠSsn, Salary – Deduction, 2000* Years_service, 0.25*Salary (EMPLOYEE)

B.S.Shankar, Dept of MCA, VVIET 88


Aggregate functions and Grouping

• To specify mathematical aggregate functions on


collections of values from the database
• Sum, Average, Maximum and Minimum can be applied to
collections of numeric values.
• The COUNT function is used to count tuples or values.
– avg: average value
– min: minimum value
– max: maximum value
– sum: sum of values
– count: number of values

B.S.Shankar, Dept of MCA, VVIET 89


Aggregate functions and Grouping

• Tuples can be grouped in a relation by the value of some


of their attributes and then applying an aggregate
function independently to each group.

• An AGGREGATE FUNCTION operation, using the symbol ƺ


(pronounced ‘script F’), to specify these types of
operations as follows:

• <grouping attributes> ƺ <function list> (R)

B.S.Shankar, Dept of MCA, VVIET 90


Aggregate functions and Grouping

• Where <grouping attributes>is a list of attributes of the


relation specified in R for which grouping is required,
• and <function list> is a list of (<function><attributes>)
pairs.
• <grouping attributes> ƺ <function list> (R)
• Dno ƺ COUNTSsn (EMPLOYEE)
• Dno ƺ COUNTSsn , AVERAGESalary(EMPLOYEE)

B.S.Shankar, Dept of MCA, VVIET 91


Aggregate functions and Grouping

• Dno ƺ COUNTSsn, AVERAGESalary(EMPLOYEE)

• Result:
R(DNO, COUNT_SSN, AVERAGE_SALARY)

• ƺ COUNTSsn, AVERAGESalary (EMPLOYEE)

• Result:
R(COUNT_SSN, AVERAGE_SALARY)

B.S.Shankar, Dept of MCA, VVIET 92


Aggregate functions and Grouping

• Retrieve each department number, the number of the


employee in the department, their average salary, while
renaming the resulting attributes

• ρR(DNO, NO_OF_EMPS, AVG_SAL)


(DNO ƺ COUNTSsn,AVERAGESalary(EMPLOYEE))

• Result: R(DNO, NO_OF_EMPS, AVG_SAL)

B.S.Shankar, Dept of MCA, VVIET 93


Recursive closure operation

• Recursive closure operation is applied to a recursive


relationship between tuples of the same type

• E.g. the relationship between an employee and a


supervisor.

• This relationship is described by the foreign key


SUPERSSN of the EMPLOYEE relation,

• And it relates each tuple of EMPLOYEE (in the role of


supervisee) to another tuple of EMPLOYEE (in the role of
supervisor).
B.S.Shankar, Dept of MCA, VVIET 94
Recursive closure operation

• Retrieve all supervisees of an employee e at all levels-that


is, all employees e’; all employees e” directly supervised
by each employee e”; and so on.

B.S.Shankar, Dept of MCA, VVIET 95


Outer Join Operations

• Outer joins are used to keep all the tuples in R, or all


those in S, or all those in both relations in the result of
the JOIN, regardless of whether or not they have
matching tuples in the other relation.
• This satisfies the need of queries in which tuples from
two tables are to be combined by matching
corresponding rows, but without losing any tuples for lack
of matching values.
• The join operations where only matching tuples are kept
in the result, are called inner joins.

B.S.Shankar, Dept of MCA, VVIET 96


Outer Join Operations ?
loan borrower
loan_number branch_name amount customer_name loan_number
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155

B.S.Shankar, Dept of MCA, VVIET 97


Outer Join Operations

• LEFT OUTER JOIN operation keeps every tuple present in


the first (left) relation in the result of
R S.

• If no matching tuple is found in S, then the attributes of S


in the join result are filled with null values.

B.S.Shankar, Dept of MCA, VVIET 98


Outer Join Operations
loan borrower
loan_number branch_name amount customer_name loan_number
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155

Left Outer Join


loan loan.loan_number=borrower.loan_number borrower

B.S.Shankar, Dept of MCA, VVIET 99


Outer Join Operations
loan borrower
loan_number branch_name amount customer_name loan_number
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155

Left Outer Join


loan loan.loan_number=borrower.loan_number borrower

loan_number branch_name amount customer_name loan_number


L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 null null

B.S.Shankar, Dept of MCA, VVIET 100


Outer Join Operations

• RIGHT OUTER JOIN keeps every tuple present in the


second (right) relation S in the result of
R S.

B.S.Shankar, Dept of MCA, VVIET 101


Outer Join Operations

loan borrower
loan_number branch_name amount customer_name loan_number

L-170 Downtown 3000 Jones L-170


L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155

Right Outer Join


loan loan.loan_number=borrower.loan_number borrower

B.S.Shankar, Dept of MCA, VVIET 102


Outer Join Operations

loan borrower
loan_number branch_name amount customer_name loan_number

L-170 Downtown 3000 Jones L-170


L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155

Right Outer Join


loan loan.loan_number=borrower.loan_number borrower

loan_number branch_name amount customer_name loan_number


L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
null null null Hayes L-155

B.S.Shankar, Dept of MCA, VVIET 103


Outer Join Operations

• FULL OUTER JOIN keeps all tuples present in both the left
and the right relations.
R S

B.S.Shankar, Dept of MCA, VVIET 104


Outer Join Operations
loan borrower
loan_number branch_name amount customer_name loan_number
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155

Full Outer Join


loan loan.loan_number=borrower.loan_number borrower

B.S.Shankar, Dept of MCA, VVIET 105


Outer Join Operations
loan borrower
loan_number branch_name amount customer_name loan_number
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155

Full Outer Join


loan loan.loan_number=borrower.loan_number borrower

loan_number branch_name amount customer_name loan_number


L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 null Null
null null null Hayes L-155
B.S.Shankar, Dept of MCA, VVIET 106
Outer Union Operations

• This is Union of two tables which are not union


compatible.

• The attributes that are union compatible are represented


only once in the result, and those attributes that are not
union compatible from either relation are also kept in the
result relation T(X,Y,Z)

B.S.Shankar, Dept of MCA, VVIET 107


Outer Union Operations

• STUDENT (Ssn, Name, Department, Advisor)


• INSTRUCTOR (Ssn, Name, Department, Rank)

Result of Outer Union


• STUDENT_OR_ INSTRUCTOR (Ssn, Name, Department,
Advisor, Rank)
• Tuple with (Ssn, Name, Department) same combination
will appear once in the result.
• Tuples appearing only in STUDENT, will have NULL for
Rank.
• Tuples appearing only in INSTRUCTOR, will have NULL for
Advisor.
B.S.Shankar, Dept of MCA, VVIET 108
B.S.Shankar, Dept of MCA, VVIET 109
Examples of queries

Q1: Retrieve the name and address of all employees


who work for the 'Research' department.
Solution:
• RESEARCH_DEPT <- σ DNAME='Research' (DEPARTMENT)

• RESEARCH_EMPS <- (RESEARCH_DEPT DNUMBER = DNO


EMPLOYEE)

• RESULT <- π fname, lname, address (RESEARCH_EMPS)

B.S.Shankar, Dept of MCA, VVIET 110


Examples of queries

Q2: For every project located in ‘Stafford’, list the


project number, the controlling department
number, and the department manager’s last
name, address and birth date.

B.S.Shankar, Dept of MCA, VVIET 111


Examples of queries

Solution:
STAFFORD_PROJS<- σ PLOCATION=‘Stafford‘ (PROJECT)

• CONTR_DEPT <- (STAFFORD_PROJS DNUM = DNUMBER


DEPARTMENT)

• PRJ_DPT_MGR <- CONTR_DEPT MGRSSN = SSN EMPLOYEE)

• RESULT <- Π PNUMBER,DNUM, LNAME, ADDRESS,BDATE


(PRJ_DPT_MGR)

B.S.Shankar, Dept of MCA, VVIET 112


Examples of queries

Q3: Find the names of employee who work on all the


projects controlled by department no. 5

B.S.Shankar, Dept of MCA, VVIET 113


Examples of queries

Solution:
DEPT5_PROJS(PNO)<-ΠPNUMBER (σ DNUM=5(PROJECT))

• EMP_PROJ(SSN, PNO)<- Π ESSN, PNO(WORKS_ON)

RESULT_EMP_SSNS<- EMP_PROJ ÷ DEPT5_PROJS

• RESULT <- Π LNAME, FNAME(RESULT_EMP_SSNS * EMPLOYEE)

B.S.Shankar, Dept of MCA, VVIET 114


Examples of queries

Q4: Make a list of project numbers for projects that involve


an employee whose last name is ‘Smith’, either as a
worker or as a manager of the department that controls
the project.

B.S.Shankar, Dept of MCA, VVIET 115


Examples of queries

Solution:
• SMITHS(ESSN) <- ΠSSN(σLNAME=‘Smith’(EMLPOYEE))

• SMITH_WORKER_PROJ <- ΠPNO(WORKS_ON * SMITHS)

B.S.Shankar, Dept of MCA, VVIET 116


Examples of queries

• MGRS <- (EMPLOYEE SSN=MGRSSN DEPARTMENT)

• SMITH_MANAGED_DEPTS(DNUM) <- ΠDNUMBER


(σ LNAME=‘Smith’(MGRS))

• SMITH_MGR_PROJS(PNO) <-ΠPNUMBER
(SMITH_MANAGED_DEPTS * PROJECT)

• RESULT <- (SMITH_WORKER_PROJ U SMITH_MGR_PROJS)

B.S.Shankar, Dept of MCA, VVIET 117


Examples of queries

Q5: List the names of all employees with two or more


dependents.
(Strictly speaking, this query cannot be done in the basic
relational algebra. We have to use the AGGREGATE
FUNCTION operation with the COUNT aggregate function.
We assume that dependents of the same employee have
distinct DEPENDENT_NAME values.)

B.S.Shankar, Dept of MCA, VVIET 118


Examples of queries

Solution:
• T1(SSN, NO_OF_DEPTS) <- ESSN ‫ ڻ‬COUNT DEPENDENT_NAME
(DEPENDENT)

• T2<- σNO_OF_DEPTS >=2 (T1)

• RESULT <- ΠLNAME, FNAME(T2 * EMPLOYEE )

B.S.Shankar, Dept of MCA, VVIET 119


Examples of queries

Q6: Retrieve the names of employees who have no


dependents.
Solution:
• ALL_EMPS <- ΠSSN( EMPLOYEE )

• EMPS_WITH_DEPS(SSN) <- ΠESSN (DEPENDENT)

• EMP_WITHOUT_DEPS <- ( ALL_EMPS - EMPS_WITH_DEPS )

• RESULT<- ΠLNAME, FNAME(EMPS_WITHOUT_DEPS * EMPLOYEE


)

B.S.Shankar, Dept of MCA, VVIET 120


Examples of queries

Q7: List the names of managers who have at least one


dependent.
Solution:
• MGRS (SSN ) <- Π MGRSSN (DEPARTMENT)

• EMPS_WITH_DPS (SSN) <- Π ESSN (DEPENDENT)

• MGRS_WITH_DEPS <- (MGRS ∩ EMPS_WITH_DPS )

• RESULT <- ΠLNAME, FNAME( MGRS_WITH_DEPS * EMPOYEE ).

B.S.Shankar, Dept of MCA, VVIET 121


End of chapter 3
(part-2)

B.S.Shankar, Dept of MCA, VVIET 122

You might also like