Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views103 pages

Module 2-RA-v2

The document provides an overview of relational algebra, a procedural query language used for database operations. It explains the types of query languages, basic operators, and specific operations like selection and projection, along with examples. Additionally, it discusses properties of these operations and the Cartesian product in relational databases.

Uploaded by

yash1215singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views103 pages

Module 2-RA-v2

The document provides an overview of relational algebra, a procedural query language used for database operations. It explains the types of query languages, basic operators, and specific operations like selection and projection, along with examples. Additionally, it discusses properties of these operations and the Cartesian product in relational databases.

Uploaded by

yash1215singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 103

June 4, 2025 Dr.

Kasturi Dhal 1
Relational Algebra
• Query: A query is a statement requesting the retrival of
information.
• Query languages: Language in which user requests
information from the database. Allow manipulation and
retrieval of data from a database.
• There are two types of formal query languages for the
relational model.
– Procedural Query language. Describe the operation how to get the
desired information.(ex- Relational Algebra)
– Nonprocedural Query language : Describe what information the
result should contain, rather than how to compute it. (ex- Relational
Calculus)
• Tuple Relational Calculus(TRC)
• Domain Relational Calculus(DRC)
June 4, 2025 Dr. Kasturi Dhal 2
Relational Algebra

• The relational algebra is a procedural query


language.
• It consists of a set of operations that take one or
two relations as input and produce a new relation
as their result.
• These operations enable a user to specify basic
retrieval requests (or queries).
• It is used to tell the DBMS how to build a new
relation from one or more existing relation.
• It forms the basis of Query language.
June 4, 2025 Dr. Kasturi Dhal 3
Relational Algebra

• Six Basic operators:


– Select
– Project
– Cartesian Product or Cross-product
– Set-difference
– Union
– Intersection
• Additional operators
– Joins (natural, equi-join, theta join, outer-join)
– Division
– Renaming
• The relation algebra operators are classified into either
– Unary: Operate on a single relation(one operator)
– Binary : Operate on two relations(two operator)
June 4, 2025 Dr. Kasturi Dhal 4
Select Operation(σ)
• Symbol: σ(sigma)
• Unary operator
• Notation :  p(r) where p is called section condition or
predicate and r is a relation
• The selection condition acts as a filter
– Keeps only those tuples that satisfy the qualifying
condition
– Tuples satisfying the condition are selected whereas
the other tuples are discarded (filtered out)
• Selects a subset of rows or tuples from relation according
to a given selection condition or predicate.
• It is used as an expression to choose tuples which meet
the selection condition.
June 4, 2025 Dr. Kasturi Dhal 5
June 4, 2025 Dr. Kasturi Dhal 6
Example
emp
eno ename sal dno
1 Jones 1000 5

2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) whose salary is 2500.
Ans: σ(sal = 2500)(emp) eno ename sal dno

2 Smith 2500 1

External Query: select * from emp where sal = 2500

June 4, 2025 Dr. Kasturi Dhal 7


Example
emp
eno ename sal dno
1 Jones 1000 5

2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) of department 3.
Ans: σ(dno = 3)(emp) eno ename sal dno

3 Blake 3700 3
4 Xyp 2900 3

June 4, 2025 Dr. Kasturi Dhal 8


Example
emp
eno ename sal dno
1 Jones 1000 5

2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) whose name is jones.
Ans: σ(ename = “jones”)(emp) eno ename sal dno

1 Jones 1000 5

June 4, 2025 Dr. Kasturi Dhal 9


Example
emp
eno ename sal dno We can combine more than one condition
1 Jones 1000 5 using AND(^), OR(v), NOT(¬)
2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) whose sal > 3000 and dno = 5
Ans: σ(sal> 3000 ^ dno = 5)(emp) eno ename sal dno

5 Pst 3785 5

June 4, 2025 Dr. Kasturi Dhal 10


Example
r
A B C D
α α 1 7
α β 5 7
β β 2 3
β β 23 10

What is the output of the following relational algebra expression?


σ(A = B ^ D > 5) (r)

A B C D
α α 1 7
β β 23 10

June 4, 2025 Dr. Kasturi Dhal 11


Example

sid sname rating age


28 yuppy 9 35.0 sid sname rating age
31 lubber 8 55.5 28 yuppy 9 35.0
44 guppy 5 35.0
58 rusty 10 35.0
58 rusty 10 35.0

S2
 rating 8(S2)

June 4, 2025 Dr. Kasturi Dhal 12


Example

• σDNO=5 (σsalary>25000 (EMPLOYEE)) == σsalary>25000 (σDno=5 (EMPLOYEE)) == σDNO=5 AND


salary>25000 (EMPLOYEE))

June 4, 2025 Dr. Kasturi Dhal 13


SELECT Operation Properties

• The SELECT operation  <selection condition>(R) produces a relation


S that has the same schema (same attributes) as R
• SELECT  is commutative:
•  <condition1>( < condition2> (R)) =  <condition2> ( < condition1> (R))
• Because of commutativity property, a cascade (sequence)
of SELECT operations may be applied in any order:
• <cond1>(<cond2> (<cond3> (R)) = <cond2> (<cond3> (<cond1> ( R)))
• A cascade of SELECT operations may be replaced by a single
selection with a conjunction of all the conditions:
• <cond1>(< cond2> (<cond3>(R)) =  <cond1> AND < cond2> AND < cond3>(R)))
• The number of tuples in the result of a SELECT is less
than (or equal to) the number of tuples in the input
relation R
June 4, 2025 Dr. Kasturi Dhal 14
Projection Operation(π)
• PROJECT Operation is denoted by  (pi)
• Unary operator
• This operation keeps certain columns (attributes) from a relation and discards the
other columns.
– PROJECT creates a vertical partitioning
• The list of specified columns (attributes) is kept in each tuple
• The other attributes in each tuple are discarded

• The general form of the project operation is:


A1,A2,....,Ak(R)
–  (pi) is the symbol used to represent the project operation
– A1,A2,...,Ak are the desired list of attributes from relation R.

• The result is defined as relation of k columns obtained by eliminating duplicate


rows from the result and erasing the columns that are not listed.

• Example: To list each employee( SSN, FNAME, LNAME, SALARY, ADDRESS, DNO)
first and last name and salary, the following is used:
LNAME, FNAME,SALARY(EMPLOYEE)
June 4, 2025 Dr. Kasturi Dhal 15
Example

S2

June 4, 2025 Dr. Kasturi Dhal 16


Example
r
A B C
α 10 1
α 20 1
β 30 1
β 40 1

What is the output of the following relational algebra expression?


π(A, C) (r)

A C
α 1
β 1

June 4, 2025 Dr. Kasturi Dhal 17


Example
emp
eno ename sal dno
1 Jones 1000 5

2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find employee name whose salary is greater
than 2500. ename
Ans: πename (σ(sal > 2500)(emp) ) Blake
Xyp
Pst

External Query: select ename from emp where sal > 2500

June 4, 2025 Dr. Kasturi Dhal 18


Example
emp
eno ename sal dno
1 Jones 1000 5

2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find employee no and salary whose salary is
greater than 3000 and dno = 3. ename sal
Ans: πename,sal (σ(sal > 3000s ^ dno=3)(emp) ) Blake 3700

June 4, 2025 Dr. Kasturi Dhal 19


Project Operation Properties

• The number of tuples in the result of


projection <list>(R) is always less or equal to
the number of tuples in R
– If the list of attributes includes a key of R, then the
number of tuples in the result of PROJECT is equal
to the number of tuples in R
• PROJECT is not commutative

June 4, 2025 Dr. Kasturi Dhal 20


Combination of Selection and Projection Operator

πSNO(σSTATE=‘NY’ (S))

O/P :

June 4, 2025 Dr. Kasturi Dhal 22


Cartesian Product or Cross Product

• Denoted by x
• Let two relational schema r(A1, A2,….., An) and s(B1, B2,….., Bm)
• Notation r x s
• Defined as: r x s = {t q | t  r and q  s}
– Assumption is that the attributes of r and s are disjoint(r Ո s = Φ)
• Each row of r combined with each row of s
• Result is a relation Q with degree n + m attributes:
• Q(A1, A2,….., An, B1, B2,….., Bm), in that order.
• Hence, if r has nr tuples (denoted as |r| = nr ), and s has ns tuples, then r x
s will have nr * ns tuples or │r x s│= nr * ns
• Also known as cross join
• It is not a meaningful operation unless it is associated with some
condition.
June 4, 2025 Dr. Kasturi Dhal 24
Cartesian Product or Cross Product

R1 X S1

June 4, 2025 Dr. Kasturi Dhal 25


Cartesian Product or Cross Product

A B C D E
α 1 α 10 a A B C D E

 1
 10 a
α 1 β 10 a  10 a
α 1 β 20 b  2  20 b
α 1  10 b r  10 b

r x s: A=C(r x s) s
β 2 α 10 a
β 2 β 10 a A B C D E
β 2 β 20 b α 1 α 10 a
β 2  20 b β 2 β 10 a
β 2 β 20 b

June 4, 2025 Dr. Kasturi Dhal 26


Cartesian Product or Cross Product

A B C D E
α 1 α 10 a A B C D E

 1
 10 a
α 1 β 10 a  10 a
α 1 β 20 b  2  20 b
α 1  10 b r  10 b

r x s: A=C(r x s) s
β 2 α 10 a
β 2 β 10 a A B C D E
β 2 β 20 b α 1 α 10 a
β 2  20 b β 2 β 10 a
β 2 β 20 b

June 4, 2025 Dr. Kasturi Dhal 27


Cartesian Product or Cross Product
stud result
ID NAME ID1 CGPA

1 XYZ 1 9.2

2 PQR 2 8.5

Stud X result
ID NAME ID1 CGPA

1 XYZ 1 9.2

1 XYZ 2 8.5
Not Valid
2 PQR 1 9.2

2 PQR 2 8.5

Write the relational algebra expression to find student ID, name and CGPA.
Ans: πNAME,ID,CGPA (σ(ID = ID1)(stud x result) ) ID NAME CGPA

Cartesian product is meaningless unless it is associated with select 1 XYZ 9.2


condition.
2 PQR 8.5

June 4, 2025 Dr. Kasturi Dhal 28


stud result
ID NAME ID1 CGPA

1 XYZ 1 9.2

2 PQR 2 8.5

• Find the name of those student whose CGPA is >9.0


The relational algebra expression π NAME (σcgpa > 9.0( stud × result) ) produces
spurious tuples leading to incorrect result.
ID NAME ID1 CGPA
ID NAME ID1 CGPA NAME
1 XYZ 1 9.2
1 XYZ 1 9.2 XYZ
1 XYZ 2 8.5
2 PQR 1 9.2 PQR
2 PQR 1 9.2

2 PQR 2 8.5
σcgpa > 9.0( stud × result) π NAME (σcgpa > 9.0( stud × result) )

stud × result
June 4, 2025 Dr. Kasturi Dhal 29
stud result
ID NAME ID1 CGPA

1 XYZ 1 9.2

2 PQR 2 8.5

• The correct relational algebra expression to find the name of those student whose
CGPA is >9.0 is

π NAME (σ ID=ID1 (σcgpa > 9.0( stud × result) )).

June 4, 2025 Dr. Kasturi Dhal 30


• Consider the following relational schema
Emp(eno,ename,dno,sal)
Dept(deptno,dname,dloc)
Que-Write the relational algebra expression to find the name all the
employees who works in MCA department. emp
eno ename sal dno
1 Smith 5000 10
1- Emp × Dept 2 Jones 6000 20
2- σdname = ‘MCA’ (Emp × Dept) 3 Blake 7000 10

3-σ dno = deptno (σdname = ‘MCA’ (Emp × Dept)) 4 Peter 8000 30

4-π ename (σ dno = deptno (σdname = ‘MCA’ (Emp × Dept)))


dept
deptno dname dloc
1- Emp dno =deptno Dept 10 MCA bbsr
2- σdname = ‘MCA’ (Emp dno = deptno Dept) 20 cs ctc

3-π ename (σdname = ‘MCA’ (Emp dno = deptno Dept))


30 IT rkl

June 4, 2025 Dr. Kasturi Dhal 31


σdname = ‘MCA’ (Emp dno = deptno Dept)
eno Ename sal dno deptno dname dloc
1 Smith 5000 10 10 MCA bbsr
emp
3 Blake 7000 10 10 MCA bbsr
eno Ename sal dno
1 Smith 5000 10
2 Jones 6000 20
π ename (σdname = ‘MCA’ (Emp dno = deptno Dept))
3 Blake 7000 10
Ename
4 Peter 8000 30
Smith
Blake

Emp Dept dept


dno =deptno
deptno dname dloc
eno Ename sal dno deptno dname dloc
10 MCA bbsr
1 Smith 5000 10 10 MCA bbsr
20 cs ctc
2 Jones 6000 20 20 cs ctc
30 IT rkl
3 Blake 7000 10 10 MCA bbsr
4 Peter 8000 30 30 IT rkl

June 4, 2025 Dr. Kasturi Dhal 32


Join
• denoted by
• Notation: R <join condition> S.
• R p S ≡ σp (R x S)

• A general join condition is of the form


<condition> AND <condition> AND … AND <condition>
Join = Cartesian Product + Select

• The sequence of CARTESIAN PRODECT followed by SELECT is used quite


commonly to identify and select related tuples from two relations
• A special operation, called JOIN combines this sequence into a single
operation.
• The join condition must an attribute of R and S.
• Only combination of tuples satisfying the join condition only appear in the
result whereas in the cartesian product all combination of tuples are
included in the results.
• If degree of R = n and degree of S = m then degree of R p S = m+n

June 4, 2025 Dr. Kasturi Dhal 33


Types of Join
• Equi Join
• Natural Join
• Theta join
• Outer Join
– Left Outer Join
– Right Outer Join
– Full Outer Join

June 4, 2025 Dr. Kasturi Dhal 34


Equijoin

When the join condition c contains only comparison operator = then such type of
join is called as EQUIJOIN.

Ex.

Only = operator can be used in join condition c.

Equi join is also a theta join.

June 4, 2025 Dr. Kasturi Dhal 35


sid sname rating age
sid bid day
R1 22 dustin 7 45.0
22 101 10/10/96 S1
58 103 11/12/96 31 lubber 8 55.5
58 rusty 10 35.0
R1 R1.sid= s1.sid S1

sid Sname Rating age sid bid day


22 dustin 7 45.0 22 101 10/10/96
58 rusty 10 35 58 103 11/12/96

The result of equi join we have one or more pair of attributes that have identical
values in every tuple.
June 4, 2025 Dr. Kasturi Dhal 36
• Consider the following relational schema
Emp(eno,ename,dno,sal)
Dept(deptno,dname,dloc)
Que-Write the relational algebra expression to find the name and salary of all the
employees who belong to MCA department. emp
eno ename sal dno
1- Emp dno =deptno Dept 1 Smith 5000 10

2- σdname = ‘MCA’ (Emp dno = deptno Dept)


2 Jones 6000 20
3 Blake 7000 10
3-π ename (σdname = ‘MCA’ (Emp dno = deptno Dept))
4 Peter 8000 30

Emp dno =deptno Dept


eno ename sal dno deptno dname dloc
dept
1 Smith 5000 10 10 MCA bbsr deptno dname dloc
10 MCA bbsr
2 Jones 6000 20 20 cs ctc
20 cs ctc
3 Blake 7000 10 10 MCA bbsr 30 IT rkl

4 Peter 8000 30 30 IT rkl

June 4, 2025 Dr. Kasturi Dhal 37


Natural Join Operation *

• Another variation of JOIN called NATURAL JOIN


• denoted by * or was created to get rid of the second (superfluous)
attribute of an EQUIJOIN condition.
– because one of each pair of attributes with identical values is
superfluous in equijoin
• Natural join is an equi join of two relation R and S over all
common attributes.
• The standard definition of natural join requires that the two join
attributes, or each pair of corresponding join attributes, have the same
name and domain in both relations
o –NATURAL
If this isJOIN doescase,
not the not use any of the operation
a renaming comparisonisoperators.
applied first.
o There should be at least one common attribute between two relations.
o One occurrence of each common attribute is eliminated from the result.
o It performs selection forming equality on those attributes which appear in both
relations and eliminates the duplicate attributes.
June 4, 2025 Dr. Kasturi Dhal 38
Natural Join

Q  R(A,B,C,D) * S(C,D,E)
The implicit join condition includes each pair of attributes with
the same name, “AND” ed together: R.C=S.C AND R.D=S.D. Result
keeps only one attribute of each such pair: Q(A,B,C,D,E)
Example:
r = (A, B, C, D)
s = (E, B, D)
t = (A, B, C, D, E)
t= r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B  r.D = s.D (r x s))
June 4, 2025 Dr. Kasturi Dhal 39
emp emp.deptno = dept.deptno dept

eno ename sal deptno deptno dname dloc

1 Smith 5000 10 10 MCA bbsr

2 Jones 6000 20 20 cs ctc


emp
3 Blake 7000 10 10 MCA bbsr eno ename sal deptno
1 Smith 5000 10
4 Peter 8000 30 30 IT rkl
2 Jones 6000 20
3 Blake 7000 10
4 Peter 8000 30
emp * dept
eno ename sal deptno dname dloc

1 Smith 5000 10 MCA bbsr


dept
2 Jones 6000 20 cs ctc deptno dname dloc
10 MCA bbsr
3 Blake 7000 10 MCA bbsr 20 cs ctc
30 IT rkl
4 Peter 8000 30 IT rkl

June 4, 2025 Dr. Kasturi Dhal 40


Natural Join Operation – Example
• Relations r, s:
B D E A B C D E
A B C D
1 a α
α 1 α a
α 1 α a α
3 a 
 2  a
α 1 α a 
1 a 
 4  b
α 1  a α
2 b 
α 1  a
α 1  a 
3 b 
 2  b
 2  b 

r
s  r *s

June 4, 2025 Dr. Kasturi Dhal 41


Example

branch (branch_name, branch_city, assets)

customer (customer_name, customer_street, customer_city)


account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower (customer_name, loan_number)
Q) Find all loans of amount over $1200

amount > 1200 (loan)


Q) Find the loan number for each loan amount greater than $1200

πloan_number (amount > 1200 (loan))

Q) Find the name, loan no and loan amount of all customers who have a loan at the bank

 customer_name, loan-number, amount (borrower borrower.loan_number=loan.loan_number loan)

Q) Find names of all customers who have both a loan and deposit account at the bank.

 customer_name (borrower borrower.customer_name=depositor.customer_name depositor)

June 4, 2025 Dr. Kasturi Dhal 42


branch (branch_name, branch_city, assets)

customer (customer_name, customer_street, customer_city)


account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower (customer_name, loan_number)

Q) Find names of all customers who have a loan at ‘DUMDUM’ branch.

customer_name (branch_name=“DUMDUM” (borrower borrower.loan_number = loan.loan_number loan))

Q) Find names of all customers who have an account at ‘Redwood’ branch.

customer_name (branch_name=“Redwood” (depositor depositor.account_number = accoount.account_number account))

June 4, 2025 Dr. Kasturi Dhal 43


Theta Join Operation
The Theta join operation is an extension to the cross-join operation that
allows us to combine a selection and a cartesian product into a single
operation.
It defines a relation that contains tuples satisfying the predicate from the
cartesian product of R and S.
•Theta can be any general Boolean expression on r 
s = σθ (r x s)
the attributes of R and S:
• R.Ai <S.Bj AND (R. Ak=S. Bl OR R. Ap<S. Bq)
where, Ɵ may be any of the
comparison operator <, ≤, >, ≥, =, ǂ

Equi join is special type of θ join where join condition uses only = as the comparison
operator.

June 4, 2025 Dr. Kasturi Dhal 44


Car Boat
Car_model Car_price Boat_model Boat_price
Car A 20,000
Boat 1 10,000
Car B 30,000
Boat 2 40,000
Car C 50,000 Boat 3 60,000

Car Car_price > Boat_price Boat)

Car_m Car_pr Boat_ Boat_


odel ice model price
Car A 20,000 Boat 1 10,000
Car B 30,000 Boat 1 10,000
Car C 50,000 Boat 1 10,000
Car C 50,000 Boat 2 40,000

June 4, 2025 Dr. Kasturi Dhal 45


•In NATURAL JOIN and EQUIJOIN, tuples without a matching (or
related) tuple are eliminated from the join result
• Tuples with null in the join attributes are also eliminated. This amounts to
loss of information.
•A set of operations, called OUTER joins, can be used when we want 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.
•It uses NULL Values.

June 4, 2025 Dr. Kasturi Dhal 46


Left Outer Join
Matching Tuple in natural join +Extra tuple in the left relation.
Takes all the tuples in left relation that did not match with any tuple in the right relation.
The attributes of the relation for the unmatched tules filled with NULL Values

The left outer join operation keeps every tuple in the first or left relation R in R S; if
no matching tuple is found in R, then the attributes of S in the natural join result are filled
or “padded” with null values.

June 4, 2025 Dr. Kasturi Dhal 47


Right Outer Join
Matching Tuple in natural join +Extra tuple in the right relation.
Takes all the tuples in right relation that did not match with any tuple in the right relation.
The attributes of the left relation for the unmatched tuple filled with NULL Values

The left outer join operation, keeps every tuple in the second or right relation S in the
result of R S. if no matching tuple is found in S, then the attributes of R in the natural
join result are filled or “padded” with null values.
June 4, 2025 Dr. Kasturi Dhal 48
Full Outer Join
Matching Tuple in natural join +Extra tuple in both left and right relation.
FULL OUTER JOIN = LEFT OUTER JOIN + RIGHT OUTER JOIN

keeps all tuples in both the left and the right relations when no matching tuples are
found, padding them with null values as needed.

June 4, 2025 Dr. Kasturi Dhal 49


Outer Join – Example
loan loan_number branch_name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

Relation borrower customer_name loan_number


Jones L-170
Smith L-230
Hayes L-155

loan * borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith

June 4, 2025 Dr. Kasturi Dhal 50


Outer Join – Example
loan loan_number branch_name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

Relation borrower customer_name loan_number


Jones L-170
Smith L-230
Hayes L-155
loan borrower
loan_number branch_name amount customer_name

L-170 Downtown 3000 Jones


L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null

June 4, 2025 Dr. Kasturi Dhal 51


Outer Join – Example
loan loan_number branch_name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

Relation borrower customer_name loan_number


Jones L-170
Smith L-230
Hayes L-155
 Right Outer Join
loan borrower

loan_number branch_name amount customer_name


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

June 4, 2025 Dr. Kasturi Dhal 52


Outer Join – Example
loan loan_number branch_name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

Relation borrower customer_name loan_number


Jones L-170
 Full Outer Join Smith L-230
Hayes L-155
loan borrower

loan_number branch_name amount customer_name


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

June 4, 2025 Dr. Kasturi Dhal 53


UNION, INTERSECTION, SET DIFFERENCE

• All of these operations take two input relations,


which must be union-compatible:
– Same number of fields.
– Corresponding’ fields have the same type.
• Two relation R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are said to be
union or type compatible, if they satisfy following two conditions:
– Both the relations , i.e. R1 and R2 should be of same arity, i.e. they
should have same number of attributes.
– Domain of the similar attributes, i.e. domain of ith attribute of R1
and domain of ith attribute of R2, should be same. (i.e.
dom(Ai)=dom(Bi) for i=1, 2, ..., n).
• The resulting relation for R1R2 (also for R1R2, or R1–R2)
has the same attribute names as the first operand relation
R1 (by convention)
June 4, 2025 Dr. Kasturi Dhal 54
BASIC RELETIONAL ALGEBRA OPERATION FROM SET THEORY

NAME SYMBOL
UNION U
INTERSECTION ∩
SET DIFFERENCE -
CARTESIAN PRODUCT ×

June 4, 2025 Dr. Kasturi Dhal 55


• If R and S are two union compatible relation then the resultant relation P = R ∪ S is
a relation includes all tuples that are either in R or in S or in both R and S.
Duplicate tuples are eliminated.
• It is defined as R ꓴ S = { t | t ϵ R or t ϵ S}

R Rꓴ S
S
A B
A B A B
α 1
α 2 α 1
α 2
β 3 α 2
β 2
β 2
β 3

June 4, 2025 Dr. Kasturi Dhal 56


Account Loan o/p
Account_no Name Name
Jones
101 Jones Loan_no Name
Smith
103 Smith 103 Smith Leon
104 Leon 104 Leon Evan

107 Evan 106 Byran Drew

Smita
110 Drew 110 Drew
Byran
112 Smita

Write the relational algebra expression to find name of those customer who
have account or loan or both.
ΠName (Account) ꓴ ΠName (Loan)

June 4, 2025 Dr. Kasturi Dhal 57


June 4, 2025 Dr. Kasturi Dhal 58
• The result of this operation, denoted by R ∩ S,
is a relation that includes all tuples that are in
both R and S.
• It is defined as R Ո S = { t | t ϵ R and t ϵ S}

June 4, 2025 Dr. Kasturi Dhal 59


R RՈS
S
A B
A B A B
α 1
α 2 α 2
α 2
β 2 β 3

June 4, 2025 Dr. Kasturi Dhal 60


Account Loan o/p
Account_no Name Name
Smith
101 Jones Loan_no Name
Leon
103 Smith 103 Smith Drew
104 Leon 104 Leon
107 Evan 106 Byran
110 Drew 110 Drew
112 Smita

Write the relational algebra expression to find name of those customer who
have both an account and loan.
ΠName (Account) Ո ΠName (Loan)

June 4, 2025 Dr. Kasturi Dhal 61


• The result of this operation, denoted by R – S,
is a relation that includes all tuples that are in
R but not in S.
• It is defined as R - S = { t | t ϵ R and t ϵ S}

June 4, 2025 Dr. Kasturi Dhal 62


R R-S
S
A B
A B A B
α 1
α 2 α 1
α 2
β 3 β 2
β 2

June 4, 2025 Dr. Kasturi Dhal 63


Account Loan o/p
Account_no Name Name
Jones
101 Jones Loan_no Name
Evan
103 Smith 103 Smith Smita
104 Leon 104 Leon
107 Evan 106 Byran
110 Drew 110 Drew
112 Smita

Write the relational algebra expression to find name of those customer who
have but not taken a loan.
ΠName (Account) − ΠName (Loan)

June 4, 2025 Dr. Kasturi Dhal 64


Properties of UNION, INTERSECTION and SET DIFFERENCE

• Notice that both union and intersection are commutative


operations; that is
– R  S = S  R, and R  S = S  R
• Both union and intersection can be treated as n-ary
operations applicable to any number of relations as both are
associative operations; that is
– R  (S  T) = (R  S)  T
– (R  S)  T = R  (S  T)
• The minus operation is not commutative; that is, in general
– R–S≠S–R

June 4, 2025 Dr. Kasturi Dhal 65


ASSIGNMENTS

Query1-: Find the bid of red coloured boats

Πbid (σcolor = ‘Red’ (Boats))

Query 2: Find the name of sailors who have reserved boat number 2

Π sname (σbid = 2(Sailors * Reserve))

Query-3 Find the names of sailors who have reserved a red boat.

Πsname (σcolor = ‘Red’ (Boats *Reserve * Sailors) )


Query-4 Find the colors of the boats reserved by sailor ‘xyz’
Πcolor (σsname = ‘xyz’ (Sailors * Reserve * Boats) )

June 4, 2025 Dr. Kasturi Dhal 66


RENAME OPERATION (ρ)

• The RENAME operator is denoted by  (rho)


• The rename operation allows to name the
results of a relational algebra expression and
also individual
x column.
• Notation:  x (E)
– Returns the expression E under the name x emp
eno ename sal dno
A1 A2 A3 A4
1 Smith 5000 10
1 Smith 5000 10
2 Jones 6000 20
2 Jones 6000 20
3 Blake 7000 10
3 Blake 7000 10
4 Peter 8000 30
4 Peter 8000 30

June 4, 2025
 x(a1,a2,a3,a4) emp Dr. Kasturi Dhal 68
RENAME OPERATION (ρ)

• In some cases, we may want to rename the attributes of a


relation or the relation name or both
– Useful when a query requires multiple operations
• Rename operation can be expressed by any of the following
forms:
– S (B1, B2, …, Bn )(R) changes both:
• the relation name to S, and
• the column (attribute) names to B1, B1, …..Bn
– S(R) changes:
• the relation name only to S
– (B1, B2, …, Bn )(R) changes:
• the column (attribute) names only to B1, B1, …..Bn

June 4, 2025 Dr. Kasturi Dhal 69


Relational Algebra Expressions

• We may want to apply several relational algebra


operations one after the other
– Either we can write the operations as a single
relational algebra expression by nesting the
operations, or
– We can apply one operation at a time and create
intermediate result relations.
• In the latter case, we must give names to the
relations that hold the intermediate results using
assignment operator ←(left arrow).
June 4, 2025 Dr. Kasturi Dhal 70
Storing Intermediate Results in Relations

OR

FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))


(Single Relational Expression)

June 4, 2025 Dr. Kasturi Dhal 71


EMPLOYEE
Fname Mname Lname SSN Bdate Address Sex Salary SuperSSN Dno

Write relational algebra expression to display name of all employees and their
supervisors.
y ← ρd (emp)
z ← π emp.Fname,emp.Mname,emp.Lname,y.Fname,y.Mname,y.Lname (emp emp.ssn = y.SuperSSn y )

June 4, 2025 Dr. Kasturi Dhal 72


Division Operation

• Division operator is denoted by


• Notation: r s
• Suited for the queries that include the phrase for all
• It is a binary operator.
• R(Z)  S(X), where X subset Z. Let Y = Z - X (and hence Z = X  Y);
that is, let Y be the set of attributes of R that are not attributes of S.
• The result of DIVISION is a relation T(Y) that includes a tuple t if
tuples tR appear in R with tR [Y] = t, and with
• tR [X] = ts for every tuple ts in S.
• For a tuple t to appear in the result T of the DIVISION, the values in
t must appear in R in combination with every tuple in S .

June 4, 2025 Dr. Kasturi Dhal 73


June 4, 2025 Dr. Kasturi Dhal 74
June 4, 2025 Dr. Kasturi Dhal 75
sno pno pno pno pno
s1 p1 p2 p2 p1
s1 p2 p4 p2
s1 p3
B1 p4
s1 p4
B2
s2 p1 sno B3
s2 p2 s1
s3 p2 s2 sno
s4 p2 s3 s1 sno
s4 p4 s4 s4 s1

A A/B1 A/B2 A/B3


June 4, 2025 Dr. Kasturi Dhal 76
Solving the query using
Assignment operator ()

account (account_number, branch_name, balance)


depositor (customer_name, account_number)
branch (branch_name, branch_city, assets)

Query : Find all customers who have an account at all branches


located in Cuttack.

Solution : A (depositor * Account)


customer_name, branch_name

B   branch_name ( branch_city = ”Cuttack” Branch)

Result  A  B

June 4, 2025 Dr. Kasturi Dhal 77


Consider the following relational schema.
EMPLOYEE(SSN, Fname,Lname, salary,super_ssn,Dno)
WORKS_ON(ESSN,Pno,Hours)

Retrieve the names of employees who work on all the projects that ‘John Smith’
works on.
SMITH ← σ Fname=‘John’ AND Lname=‘Smith’ (EMPLOYEE)
SMITH_PNOS ← πPno(WORKS_ON Essn=Ssn SMITH)
SSN_PNOS ← πEssn, Pno(WORKS_ON)
y ← SSN_PNOS ÷ SMITH_PNOS
RESULT ← π Fname, Lname (y * EMPLOYEE)

June 4, 2025 Dr. Kasturi Dhal 78


• Consider the following relational schema.
EMPLOYEE(SSN, Fname,Lname, salary,super_ssn,Dno)
WORKS_ON(ESSN,Pno,Hours)
PROJECT(Pname,Pnumber,Plocation,Dnum)

• Find the names of employees who work on all


the projects controlled by department number
5.
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)

June 4, 2025 Dr. Kasturi Dhal 79


• Consider the following relational schema.

EMPLOYEE(SSN, Fname,Lname, salary,super_ssn,Dno)
DEPEDENT(ESSN, Dependent_name, Relationship)

Retrieve the names of employees who have no


dependents.
– ALL_EMPS   SSN(EMPLOYEE)
– EMPS_WITH_DEPS(SSN)   ESSN(DEPENDENT)
– EMPS_WITHOUT_DEPS  (ALL_EMPS - EMPS_WITH_DEPS)
– RESULT   LNAME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)

June 4, 2025 Dr. Kasturi Dhal 80


Generalized Projection
• Extends the projection operation by allowing arithmetic functions to
be used in the projection list.

F1 ,F2 ,..., Fn (E )


– where, E is any relational-algebra expression
– each of F1, F2, …, Fn are arithmetic expressions involving
constants and attributes in the schema of E.

Example :
Given relation Employee (emp_name, SSN, desgn, basic_sal, TA)
Find the total salary of each employee:
 emp_name, (basic_sal + TA) (Employee)

June 4, 2025 Dr. Kasturi Dhal 81


emp
eno sal ename
1 1000 xyz
2 5000 pqr
3 7000 abc

Write relational algebra expression to retrieve name and annual salary of employees.
ρ(ename,Annual Salary) (Π (ename, 12 * sal) (emp))

ename Annual Salary


xyz 12000
pqr 60000
abc 84000

June 4, 2025 Dr. Kasturi Dhal 82


Aggregate Functions
• Aggregation function takes a collection of values and returns a single value
as a result.
avg : average value
min : minimum value
max : maximum value
sum : sum of values
count : number of values

Syntax :
G1 ,G2 ,,Gn  F ( A ), F ( A ,, F ( A ) ( E )
1 1 2 2 n n

E is any relational-algebra expression


– G1, G2 …, Gn is a list of attributes known as grouping attributes (can be empty)
– Each Fi is an aggregate function
– Each Ai is an attribute name
• The operator is pronounced as script F.
• The resulting relation has the grouping attributes plus one attribute for each
element in the function list.
• However, NULL values are not considered in the aggregate function
June 4, 2025 Dr. Kasturi Dhal 83
–g (EMPLOYEE) retrieves the maximum salary value
max( Salary)
from the EMPLOYEE relation
–g min( Salary)(EMPLOYEE)
retrieves the minimum Salary value
from the EMPLOYEE relation
–g sum( Salary)(EMPLOYEE) retrieves the sum of the Salary from the
EMPLOYEE relation
– g count( SSN),avg(Salary) (EMPLOYEE) computes the count (number)
of employees and their average salary

June 4, 2025 Dr. Kasturi Dhal 84



Aggregate Operation – Example
Relation r :
A B C

  7
  7
  3
  10

 g sum(c) (r) sum(c )

27

If no grouping attributes are specified, the functions are applied to all the tuples
in the relation, so the resulting relation has a single tuple only.
June 4, 2025 Dr. Kasturi Dhal 85
• Write the relational algebra expression to find
the maximum salary of employees.
emp
eno sal ename deptno
1 1000 xyz d1
2 5000 pqr d2
3 7000 abc d1

max(sal)
g max(sal) (emp)
7000

June 4, 2025 Dr. Kasturi Dhal 86


• Write the relational algebra expression to
display the maximum salary of each
department.
emp
eno sal ename deptno
1 1000 xyz d1
2 5000 pqr d2
3 7000 abc d1

deptno max(sal)
deptno g max(sal) (emp) d1 7000
d2 5000

select max(sal) from emp group by deptno


June 4, 2025 Dr. Kasturi Dhal 87
Write RA expression to display deptno, avg salary and no of employees of each
department
SSN eno salary Dno

ρR(Dno, NO-of-Employees, Average-salary) (Dno g count(SSN),avg(salary) (employee)) E1 SMITH 30000 5


E2 Wang 40000 5

E3 John 25000 4
E4 Jenifer 43000 4
E5 Rames 38000 5
h

E6 Joyce 25000 5
E7 Ahmad 25000 4
E8 James 55000 1

g count(SSN),avg(salary) (employee)

June 4, 2025 Dr. Kasturi Dhal 88


Aggregate Operation – Example
• Relation account grouped by branch-name:

branch_name account_number balance


Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700

branch_name g sum(balance) (account)

branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700

June 4, 2025 Dr. Kasturi Dhal 89


EMPLOYEE(SSN, Fname,Lname, salary,super_ssn,Dno)
DEPEDENT(ESSN, Dependent_name, Relationship)

• Display the names of all employees with two or


more dependents.
– T1(Ssn, No_of_dependents) ← Essng COUNT (Dependent_name)
(DEPENDENT)
– T2 ← σNo_of_dependents>2(T1)
– RESULT ← πLname, Fname(T2 * EMPLOYEE)

June 4, 2025 Dr. Kasturi Dhal 90


Examples : Aggregate Functions

Schema :

Query 1 :

Solution :

June 4, 2025 Dr. Kasturi Dhal 91


Examples : Aggregate Functions

Schema :

Query 3 :

Solution :

June 4, 2025 Dr. Kasturi Dhal 92


Modification of Database

• The content of database may be modified using following operations:

– Deletion
– Insertion
– Updating

• All these operations are expressed using the assignment operator.

June 4, 2025 Dr. Kasturi Dhal 93


where r is a relation and E is a relational algebra expression.

June 4, 2025 Dr. Kasturi Dhal 94


account
Deletion Examples
(account_number, branch_name, balance)
loan (loan_number, branch_name, amount)

• Delete all account records of Perryridge branch.

account  account – branch_name = “Perryridge” (account )

Delete all loan records with amount in the range of 0 to 50


loan  loan – amount 0and amount  50 (loan)

Delete emp smith record from emp(eno, ename, sal, dno) relation
emp ← emp – σ ename = “smith” (emp)

Delete all employees record whose salary >= 5000nfrom emp(eno, ename, sal, dno)
relation
emp ← emp – σ sal ≥ 5000s (emp)

June 4, 2025 Dr. Kasturi Dhal 95


Insert
Syntax: r←rꓴ E

where r is a relation and E is a relational algebra expression.

It is used to insert a new record into a relation.

Write the relational algebra expression to insert a emp tuple(305,”rama”,10000,10)


into emp(eno, ename, sal,dno).
emp ← emp ꓴ {305,”rama”,10000,10 }

June 4, 2025 Dr. Kasturi Dhal 96


Insertion Examples
• Insert information in the database specifying that Smith has $1200 in account A-
973 at the Perryridge branch. Consider the following relational schema.

account (account_number, branch_name, balance)


depositor (customer_name, account_number)

account  account  {(“A-973”, “Perryridge”, 1200)}


depositor  depositor  {(“Smith”, “A-973”)}

June 4, 2025 Dr. Kasturi Dhal 97


UPDATE
A mechanism to change a value in a tuple without charging all values in the tuple

Use the generalized projection operator to do this task


r ← Π F1,, F2,….., Fn (r)
Where each Fi is either the ith attribute or updated value of ith attribute of r.

Write a relational algebra expression to increase the salary of each


employee(eno,ename,sal,dno) by 30% .
employee ← Π eno, ename, sal + 0.3 * sal,dno (employee)

June 4, 2025 Dr. Kasturi Dhal 98


Update Examples
account (account_number, branch_name, balance)

• Make interest payments by increasing all balances by 5 percent.


account   account_number, branch_name, balance * 1.05 (account)

 Pay all accounts with balances over $10,000 6 percent interest


and pay all others 5 percent

account   account_number, branch_name, balance * 1.06 ( BAL  10000 (account ))


  account_number, branch_name, balance * 1.05 (BAL  10000 (account))

June 4, 2025 Dr. Kasturi Dhal 99


Example

• Consider the following relation schema:-

• employee (person-name, street, city)


• works (person-name, company-name, salary)
• company (company-name, city)
• manages (person-name, manager-name)

June 4, 2025 Dr. Kasturi Dhal 100


Example
• Find the names of all employees who work for First Bank Corporation

• Π person-name (σ company-name = “First Bank Corporation” (works))

• Find the names and cities of residence of all employees who work for
First Bank Corporation.

• Πperson-name, city (employee *( σcompany-name = “First Bank Corporation” works))


• ≡Πperson-name, city (employee x( σcompany-name = “First Bank Corporation” works))
• ≡ Πperson-name, city (σ company-name = “First Bank Corporation” AND employee.person-name=works.person-name employee x works)

• ≡ Πperson-name, city (σ company-name = “First Bank Corporation” employee employee.person-name=works.person-name works)

June 4, 2025 Dr. Kasturi Dhal 101


Example

• Find the names, street address, and cities of residence of all


employees who work for First Bank Corporation and earn more
than $10,000 .
• Πperson-name, street, city(σ(company-name = “First Bank Corporation” ∧ salary > 10000) (works) *
employee)

• Find the names of all employees in this database who live in the
same city as the company for which they work.
• Πperson-name (employee * works * company)

June 4, 2025 Dr. Kasturi Dhal 102


Example

• Give all employees of First Bank Corporation a 10 percent salary


raise.
• works ← Πperson-name,company-name,1.1∗salary(σ(company-name=“First Bank Corporation”)
(works)) ∪ (works − σcompany-name=“First Bank Corporation”(works))

June 4, 2025 Dr. Kasturi Dhal 103


Example

• Consider the following relational schema

June 4, 2025 Dr. Kasturi Dhal 104


Example

• Query 1. Retrieve the name and address of all employees


who work for the ‘Research’ department.
– RESEARCH_DEPT ← σDname=‘Research’(DEPARTMENT)
– RESEARCH_EMPS ← (RESEARCH_DEPT Dnumber=Dno EMPLOYEE)
– RESULT ← πFname, Lname, Address(RESEARCH_EMPS)

• Query-2 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.
– STAFFORD_PROJS ← σPlocation=‘Stafford’(PROJECT)
– CONTR_DEPTS ← (STAFFORD_PROJS Dnum=Dnumber DEPARTMENT)
– PROJ_DEPT_MGRS ← (CONTR_DEPTS Mgr_ssn=Ssn EMPLOYEE)
– RESULT ← πPnumber, Dnum, Lname, Address, Bdate(PROJ_DEPT_MGRS)

June 4, 2025 Dr. Kasturi Dhal 105


Example

• Query 3. 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.
– SMITHS(Essn) ← πSsn (σLname=‘Smith’(EMPLOYEE))
– SMITH_WORKER_PROJS ← πPno(WORKS_ON * SMITHS)
– MGRS ← πLname, Dnumber(EMPLOYEE Ssn=Mgr_ssn DEPARTMENT)
– SMITH_MANAGED_DEPTS(Dnum) ← πDnumber (σLname=‘Smith’(MGRS))
– SMITH_MGR_PROJS(Pno) ← πPnumber(SMITH_MANAGED_DEPTS *
PROJECT)
– RESULT ← (SMITH_WORKER_PROJS ∪ SMITH_MGR_PROJS)
June 4, 2025 Dr. Kasturi Dhal 106

You might also like