0% found this document useful (0 votes)
70 views62 pages

Relational Algebra

Relational algebra is a formal system for querying and manipulating data in relational databases, consisting of operations that return relations as results. Key operations include selection, projection, union, set difference, and Cartesian product, each with specific characteristics and applications. These concepts form the foundation for SQL and are essential for implementing and optimizing database queries.

Uploaded by

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

Relational Algebra

Relational algebra is a formal system for querying and manipulating data in relational databases, consisting of operations that return relations as results. Key operations include selection, projection, union, set difference, and Cartesian product, each with specific characteristics and applications. These concepts form the foundation for SQL and are essential for implementing and optimizing database queries.

Uploaded by

Deepanshu Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Relational Algebra

Relational Algebra

• Relational algebra is a set of operations that enable the


user to specify basic retrieval requests. Each operation
produces results which is a relation.
• Relational algebra expression is a sequence of relational
algebra operations whose result will also be a relation.
• It provides a formal foundation of relational model
• It is used as a basis for implementing and optimizing
queries in relational DBMS
• Some of its concept incorporated into the SQL for RDBMS
Fundamental Operations
•SELECTION / RESTRICTION
• PROJECTION
• UNION
• SET DIFFERENCE
•CARTESIAN-PRODUCT

•Operations may be of two types depending upon the


number of input relations
•Unary - Operate on one relation
•Binary - Operate on pair of relations
Notion of selection operation
•It is denoted by
σ <selection condition>(R)
•It is a unary operation that selects a sub set of the tuples
from a relation, which satisfies a given predicate.
•The SELECT operation can also be visualized as a horizontal
partition of the relation into two sets of tuples – one that
satisfies the condition & those that do not satisfy the
condition.
•Predicate appears as a subscript to sigma(σ).
•Relational operators can be used in selection predicates for
comparisons.
•Several predicates can be combined into one predicate using
AND () or OR(V)
Select Operation
• It is denoted by:  p(r)
• It is considered as a filter that keeps only those tuples
that satisfies a qualifying condition.
• p is called the selection predicate (SELECT condition)
• Defined as:
p(r) = {t | t  r and p(t)}
Where p is a formula in propositional calculus consisting
of terms connected by :  (and),  (or),  (not)
Each term is one of:
<attribute>op <attribute> or <constant>
where op is one of: =, , >, , <, 
• Example of selection:  name=“Anil” (EMPLOYEE)
Selection
--- Select Operation – Example

A B C D A B C D

  1 7 A=B ^ D > 5 (r)   1 7


  5 7   23 10
  12 3
  23 10

r
Example

Select all loans whose loan amount is greater than repaid amount

branch-name repayments amount

Delhi 1000 1500

Delhi 800 1300

σ amount > repayments(loan)


Characteristics of SELECT Operation
• The select condition is applied independently to each tuple t in
r. If the condition is true, then tuple t is selected and will
appear in the resulting relation.
• The SELECT operation is unary, it is applied to a single relation.
• The degree of resulting relation is the same as r.
• The cardinality of resulting relation is less than or equal to r.
i.e. |  c(R)| <= |R|
• The SELECT operation is cumulative. A sequence of SELECT
operations can be applied in any order.
– <cond1>(<cond2> (r)) = <cond2>( <cond1>(r))
• A cascade of SELECT operations can be combined into a single
SELECT operation with a conjunctive (^) condition.
– <cond1>(…(<condn> (r)) = <cond1>^<cond2> …<condn>(r)
Example
To select the EMPLOYEE tuples whose department is 4
EMPLOYEE(Eid, Ename, Salary, Dno)

 Dno = 4 (EMPLOYEE)
To select the EMPLOYEE tuples whose salary is greater than 10,000
 Salary > 10000 (EMPLOYEE)
To select the EMPLOYEE tuples whose dapartment is 4 and salary is
greater than 10,000

 Dno = 4 ( salary >1000 (EMPLOYEE))


or

 Salary >1000 ( Dno = 4 (EMPLOYEE))


or
 Salary >1000 AND Dno = 4 (EMPLOYEE))
Example
To select the EMPLOYEE tuples who either work in department
4 and make over Rs 15,000 per month or work in department 5
and make over Rs. 10,000 per month

 (Dno =4 AND salary >=15000) OR (Dno =4 AND salary >=10000) (EMPLOYEE)


Project Operation
• Is used to select some attributes from a relation.

• Is denoted by:
<attribute list>(r)

where <attribute list> are attribute names and r is a relation algebra


expression

• The result is defined as the relation of <attribute list> columns


obtained by erasing the columns that are not listed, and in the same
order as they appear in the list.

• Example: To eliminate the name attribute of DEPARTMENT

number (DEPARTMENT)
Projection
Project Operation – Example

A B C A C

 10 1  1
A,C (r)
 20 1  1
 30 1  2
 40 2
r
Duplicates Removed
Characteristics of PROJECT Operation
• The result of a PROJECT operation will be a relation consisting of
the attributes specified in the <attribute list> in the same order.

• The degree is equal to the number of attributes in the list.

• The projection operations removes any duplicates.

• The cardinality of the resulting relation is always less than or equal


to the cardinality of r.

• For a cascade of PROJECT operations, only the outermost need to


be considered for evaluation. If <list1>  <list2>  …  <listn>  r,
then
– <list1>(<list2>(… (<listn>(r))) = <list1>(r)
Rename Operation
• The rename operation ( )(Rho) allows us to name, and
therefore to refer to, the results of relational-algebra
expressions.
• Allows us to refer to a relation by more than one name.
Example:
 s (r)
returns the expression r under the name s

• If a relational-algebra expression r has arity n, then


s (A1, A2, …, An) (r)
returns the result of expression r under the name s, and
with the
attributes renamed to A1, A2, …., An.
Rename Operations: Example

s A B C

 a 
 
r s (r) 
a
a 
A B C

 a  (D, E, F ) (s)
 a 
 a  s(D, E, F ) (r)

s D E F

 a 
 a 
 a 
Rename Example
To retrieve the first name, last name, and salary of all employees
who work in department number 5.
The single relational algebra expression as follows

 Fname,Lname,salary ( Dno = 5(EMPLOYEE))

Alternatively we can explicitly show the sequence of operations, giving a name to


each intermediate relation

DEP5_EMP  Dno = 5(EMPLOYEE)

Result  Fname, Lname, salary(EMPLOYEE) Rename of Relation

Result(FIRSTNAME , LASTNAME , SALARY)  Fname , Lname , salary(DEP5_EMP)

Rename of Attributes
Union Operation
• Is denoted by: r  s
• Is defined as:
r  s = {t | t  r or t  s}

• The result of r  s will include all tuples which are either in r


or in s or in both.

• For r  s to be valid r and s must be union compatible


• Union compatible – Two relations R(A1, A2, ……..,An) and
S(B1, B2, ……..Bn ) are said to be union compatible if they have
the same degree n, and if dom(Ai) = dom(Bi) for 1<= i <= n.
This means that the two relations have the same number of
attributes and that each pair of corresponding attributes have
the same domain.
Union Operation

• Union operation is:


– Commutative: r s=s r
– Associative: r  (s  w) = (r  s)  w

• E.g. to find all the names of faculty and students in the


FACULTY and STUDENT tables:
name (FACULTY)  name (STUDENT)
Union Operation – Example

A B A B A B

 1  2 rs  1
 2  3  2
 1  1
s
r  3

No Duplicates
Set Difference Operation

• Is denoted by: r – s
• Is defined as: r – s = {t | t  r and t  s}
• The result of r – s will include all the tuples
that are in r but not in s.
• r and s must be union compatible
• This operation is neither Commutative nor
Associative.
Set Difference Operation – Example

A B A B A B

 1  2 r–s  1
 2  3  1
 1
s
r
Set-Intersection Operation
• Is denoted by: r  s
• Is defined as: r  s ={ t | t  r and t  s }
• The result of r  s will include all the tuples
that are in both r and s.
• r and s must be union compatible.
• Intersection is:
– Commutative: rs =sr
– Associative: r  (s  w) = (r  s)  w
• Note: r  s = r - (r - s)
Set-Intersection Operation - Example

A B A B A B
 1  2
rs  2
 2  3
 1
s
r
Cartesian-Product Operation
• Is denoted by: r x s, also called cross product
• Is defined as:
r x s = {t q | t  r and q  s}
• The result of r x s will combine tuples from both
r and s in a combinatorial fashion.
• Assume that attributes of r(A) and s(B) are
disjoint. (That is, A  B = ).
• If attributes of r(A) and s(B) are not disjoint, then
renaming must be used.
Cartesian-Product Operation-Example

A B C D E A B C D E

 1  10 a  1  10 a
 10 a rxs  1  10 a
 2  
 20 b 1 20 b
r  10 b  1  10 b
 2  10 a
s  2  10 a
 2  20 b
 2  10 b
Example Cartesian Product
EMPLOYEE
NAME SSN GENDER SALARY SUPERSSN
John 123456789 M 30000 333445555
Franklin 333445555 M 40000 888665555
Alicia 999887777 F 25000 987654321
Jennifer 987654321 F 43000 888665555
Ramesh 666884444 M 38000 333445555
Joyce 453453453 F 25000 333445555
Ahmad 987987987 M 25000 987654321
James 888665555 M 55000 Null

ESSN NAME GENDER RELATIONSHIP


DEPENDENT
333445555 Robort M Son
987654321 Abner M Son
123456789 Alice F Daughter
FEMALE_EMP
Steps
NAME SSN GENDER SALARY SUPERSSN
Alicia 999887777 F 25000 987654321
Jennifer 987654321 F 43000 888665555
Joyce 453453453 F 25000 333445555
EMPNAMES
NAME SSN
Alicia 999887777
Jennifer 987654321
Joyce 453453453
EMP_DEPENDENTS Steps
NAME SSN ESSN NAME GENDER ….
Alicia 999887777 333445555 Robort M ….
Alicia 999887777 987654321 Abner M ….
Alicia 999887777 123456789 Alice F ….
Jennifer 987654321 333445555 Robort M
Jennifer 987654321 987654321 Abner M ….
Jennifer 987654321 123456789 Alice F ….
Joyce 453453453 333445555 Robort M ….
Joyce 453453453 987654321 Abner M ….
Joyce 453453453 123456789 Alice F ….
ACTUAL_DEPENDENTS

NAME SSN ESSN NAME GENDER ….


Jennifer 987654321 987654321 Abner M ….
RESULTS

NAME NAME
Jennifer Abner
Characteristics of Cartesian-Product Operation

• Degree r X s = degree(r) + degree(s)

• Cardinality of r X s = cardinality(r) * cardinality(s)

• Generally the result of CARTESIAN PRODUCT is


meaningless unless is followed by SELECT, and
is called JOIN.
JOIN Operation
• Join Operation combine related tuples from
two relations into a single tuple based on join
condition.
• Its is denoted by: r <join condition>s
Composition of Operations
• Can build expressions using multiple operations
• Example: σA=C(r x s)
• rxs σA=C(r x s)
Characteristic of the Join Operation

• Degree of the r s = degree(r) + degree(s).


• Cardinality of r s is between 0 and
cardinality (r) * cardinality(s).
• The order of attributes in r s is { A1, A2, …, An,
B1, B2, …, Bm} where A1, A2, …, An attributes of r
and B1, B2, …, Bm are attributes of s.
• The resulting relation has one tuple for each
combination of tuples – one from r and one for s
– whenever the combination satisfies the join
condition.
Types of Join Operation
• Theta join
• Equijoin
• Natural join
• Outer Join
– Left Outer Join
– Right Outer Join
– Full Outer Join
Tables Used in Coming Examples
 The following two tables will be used in coming examples.

loan-number branch-name amount


loan L-170 Ghaziabad 30000
L-230 Meerut 40000
L-260 Shaibabad 17000

customer-name loan-number
Rajan L-170
borrower Naresh L-230
Seema L-155
Theta Join

• Its is denoted by: r <r.A θ s.B> s

Where θ = {=, ≠, < , >, ≤, ≥}

loan loan-number = loan-number Borrower

Loan-number Branch-name amount Customer-name Loan-number

L-170 Ghaziabad 3000 Rajan L-170


L-230 Meerut 4000 Naresh L-230
Equijoin
• The most common join involves join conditions with equality comparisons, where
θ is {=}. This special type of Theta join is called Equijoin.

• Its is denoted by: r <r.A = s.B> s

loan loan-number = loan-number Borrower

Loan-number Branch-name amount Customer-name Loan-number

L-170 Ghaziabad 3000 Rajan L-170


L-230 Meerut 4000 Naresh L-230

 The problem with Equijoin is Pairs of attributes with identical values in evey tuple.
Example of EquiJoin

The Relational Algebra Expression can be written as

Table1 <Table1.City = Table2.city> Table2


Natural-Join Operation
 Is denoted by: r * s
 Let r and s be relations on schemas R and S
respectively.
Then, r * s is a relation on schema R  S obtained as
follows:
– Consider each pair of tuples tr from r and ts from s.
– If tr and ts have the same value on each of the
attributes in R  S, add a tuple t to the result,
where
• t has the same value as tr on r
• t has the same value as ts on s
Natural-Join Operation

• Example:
R = (A, B, C, D)
S = (E, B, D)
– Result schema = (A, B, C, D, E)
– 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))
Natural Join Operation – Example 1

A B C D B D E A B C D E
 1  a 1 a  r*s  1  a 
 2  a 3 a   1  a 
 4  b 1 a   1  a 
 1  a 2 b   1  a 
 2  b 3 b   2  b 
r s

Look for: r.B=s.B ^ r.D = s.D


Natural Join – Example 2

loan * Borrower

loan-number branch-name amount customer-name


L-170 Ghaziabad 3000 Rajan
L-230 Meerut 4000 Naresh

 Unlike Equijoin, no pairs of attributes with identical values in evey tuple.


Outer Join
• An extension of the join operation that avoids
loss of information.
• Computes the join and then adds tuples form
one relation that does not match tuples in
the other relation to the result of the join.
• Uses null values:
– null signifies that the value is unknown or does
not exist
Left Outer Join – Example
Left outer join includes all records from first(left) of two relations

The relational Algebra Expression

Table1 <Table1.City = Table2.city> Table2


Right Outer Join – Example
Right outer join includes all records from second(Right) of two relations

The relational Algebra Expression

Table1 <Table1.City = Table2.city> Table2


Full Outer Join
• It is neither "left" nor "right"— it's both! It
includes all the rows from both of the tables or
result sets participating in the JOIN. When no
matching rows exist for rows on the "left" side of
the JOIN, you see Null values from the result set
on the "right” and vice versa.

• If 2 joined tables contain M and N rows, then


cross join will always produce (M x N) rows, but
full outer join will produce from MAX(M,N) to (M
+ N) rows.
OUTER UNION Operation

• If the relations are not UNION Compatible.

•It operate on Partially Union Compatible relations.

For example R(X, Y) and S(X, Z) Result T(X, Y, Z)

•Same combination appears only ONCE in the result

•For example STUDENT(Name, SSN, Department, Advisor)


INSTRUCTOR(Name, SSN, Department, Rank)

STUDENT_OR_INSTRUCTOR(Name, SSN, Department, Advisor, Rank)


Division Operation
• Is denoted by: r  s

• Suited to queries that include the phrase “for all”.

• Let r and s be relations on schemas R and S respectively where


– R = (A1, …, Am, B1, …, Bn)
– S = (B1, …, Bn)
The result of r  s is a relation on schema R – S = (A1, …, Am)

r  s = { t | t   R-S(r)   u  s ( tu  r )
}
in A/B, the attributes in B must be included in
the schema for A. Also, the result has
attributes A-B.
Examples of Division A/B
sno pno pno pno pno
s1 p1 p2 p2 p1
s1 p2 p4 p2
s1 p3
B1 p4
B2
s1 p4
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


Division Operation – Example1

A B B A

 1 1 rs 
 2
 3
2 
 1 s
 1
 1
 3
 4
 6
 1
 2

r
Division Operations: Example 2

A B C D E D E A B C
 a  a 1 a 1 rs  a 
 a  a 1 b 1  a 
 a  b 1
 a  a 1
s
 a  b 3
 a  a 1
 a  b 1
 a  b 1
r
Division Operation
• Property
– Let q = r  s
– Then q is the largest relation satisfying q x s  r
• Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S  R

r  s = R-S (r) –R-S ( (R-S (r) x s) – R-S,S(r))

To see why
– R-S,S(r) simply reorders attributes of r

– R-S(R-S (r) x s) – R-S,S(r)) gives those tuples t in

R-S (r) such that for some tuple u  s, tu  r.


Tables Used in Coming Examples
 The following two tables will be used in coming examples.

loan
loan-number branch-name amount
L-170 Ghaziabad 30000
L-230 Meerut 40000
L-260 Shaibabad 17000

borrower Depositor
customer-name loan-number customer-name Acc-number
Rajan L-170 Raj S-150
Naresh L-230 Rahul S-250
Seema L-155 Seema S-150
Sanjay S-221
Example Queries

• Find all loans of over $1200

amount > 1200 (loan)

 Find the loan number for each loan of an amount greater than $1200

loan-number (amount > 1200 (loan))


Example Queries
• Find the names of all customers who have a loan, an account, or both, from the bank

customer-name (borrower)  customer-name (depositor)

 Find the names of all customers who have a loan and an account at bank

customer-name (borrower)  customer-name (depositor)


Example Queries
• Find the names of all customers who have a loan at the KFUPM branch.

customer-name (branch-name=“KFUPM” (borrower * loan))

 Find the of all customers who have a loan at the KFUPM branch but do not have an
account at any branch of the bank

customer-name (branch-name = “KFUPM” (borrower * loan)) –


customer-name(depositor)
Example Queries
• Find the names of all customers who have a loan at the KFUPM branch.

 Query 1
customer-name(branch-name = “KFUPM” (borrower * loan))
Example Queries

• Find the largest account balance. Rename account relation as d

balance(account) - account.balance
(account.balance < d.balance (account * d (account)))
Example Queries
• Find all customers who have an account from at least the “Dammam” and the “Khobar”
branches.

Query 1

CN(BN=“Dammam”(depositor * account)) 
CN(BN=“Khobar”(depositor * account))

where CN denotes customer-name and BN denotes branch-name.

Query 2
customer-name, branch-name (depositor * account)

 temp(branch-name) ({(“Dammam”), (“Khobar”)})


Example Queries

• Find all customers who have an account at all


branches located in Dammam city.

customer-name, branch-name (depositor * account)


 branch-name (branch-city = “Dammam” (branch))

You might also like