You are on page 1of 27

Relational Algebra

and Queries
Relational Model
 A relational model or database consists of multiple relations having
relationship between them
 Relation: A table consist of rows and columns.
 E.g. Employee, Customer etc.
 Attribute : Every column of a relation is referred to as an attribute.
 E.g. ssn, empname
 Domain : A range of acceptable values for particular attribute is
called domain.

 E.g. ssn number , empname char(10)


 Tuples : the rows of a relation or record.
 Eg.
ssn empname
111 john
112 Joseph
Relational Algebra
 A formal and high procedure language for relational model.
 The basic set of operations for the relational model is the relational
algebra.
 Basic operations:
 Selection (
) Selects a subset of rows from relation.
Projection (
) Deletes unwanted columns from relation.


 Cross-product ( ) Allows us to combine two relations.


 Set-difference ( - ) Tuples in relation 1, but not in relation 2.
 Union (  ) Tuples in relation 1 and in relation 2.
 Rename ( ) Allows to rename a relation
Unary Operator - Selection
 Selects rows that satisfy selection condition.
 Schema of result identical to schema of (only) input relation.

 <selection condition>(R)

S2
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0  rating 8(S2)
sid sname rating age
28 yuppy 9 35.0
58 rusty 10 35.0
Selection

 used to select a subset of the tuples


SELECT Operation:
from a relation that satisfy a selection condition.
It is a filter that keeps only those tuples that
satisfy a qualifying condition.
Examples:
 DNO = 4 (EMPLOYEE)
 SALARY > 30,000 (EMPLOYEE)

 denoted by <selection condition>(R) where the symbol 


(sigma) is used to denote the select operator, and the
selection condition is a Boolean expression specified on
the attributes of relation R
Selection Condition

 Operators: <, , , >, =, 


 Simple selection condition:
 <attribute> operator <constant>
 <attribute> operator <attribute>

 <condition> AND <condition>


 <condition> OR <condition>
 NOT <condition>
Select Examples
Person Id Name Address Hobby
1123 John 123 Main stamps
1123 John 123 Main coins
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps
 Id>3000 OR Hobby=‘hiking’ (Person)

 Id>3000 ν Hobby=‘hiking’ (Person)

 Id>3000 AND Id <3999 (Person)


 Id>3000 ^ Id <3999 (Person)

 NOT(Hobby=‘hiking’) (Person)
 Hobby‘hiking’ (Person)
Unary Relational Operations
 selects certain columns from the
PROJECT Operation:
table and discards the others.
Example:

 LNAME, FNAME,SALARY (EMPLOYEE)

The general form of the project operation is:


<attribute where  is the symbol used to
list>(R)
represent the project operation and <attribute list>
is the desired list of attributes.
 PROJECT removes duplicate tuples, so the result is a
set of tuples and hence a valid relation.
Unary Operator - Projection

 Deletes attributes that are not in projection list.

sname rating  sname,rating(S2)


yuppy 9
lubber 8
guppy 5 age
rusty 10
35.0
55.5
 age(S2)
Composition of Operations

 Selection and projection


 Result relation can be the input for another relational
algebra operation! (Operator composition.)

sname rating
 sname,rating( rating 8(S2)) yuppy 9
rusty 10
SELECT and PROJECT Operations

(a) (DNO=4 AND SALARY>25000) OR (DNO=5 AND


SALARY>30000)(EMPLOYEE)
(b) LNAME, FNAME, SALARY(EMPLOYEE)
(c) SEX, SALARY(EMPLOYEE)
Rename Operations

 Rename operation not only renames the relation but


also renames the attributes of that relation.
ρx
ρ account
ρ Less bal

 Renaming operator:  (C(1 sid1, 5  sid 2), S1 R1)


Set Operators - Union, Intersection, Set-
Difference

 All of these operations


take two input relations, S1 sid sname rating age
which must be union- 22 dustin 7 45.0
compatible: 31 lubber 8 55.5
 Same number of fields.
58 rusty 10 35.0
 `Corresponding’ fields have the
same type.
S2 sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
Union, Intersection, Set-Difference

sid sname rating age


22 dustin 7 45.0
31 lubber 8 55.5
S1 S2
58 rusty 10 35.0
44 guppy 5 35.0
28 yuppy 9 35.0

sid sname rating age sid sname rating age


31 lubber 8 55.5
22 dustin 7 45.0
58 rusty 10 35.0
S1 S2 S1 S2
Binary Operation - Cross-Product

 If R and S are two relations, R  S is the set of all


concatenated tuples <x,y>, where x is a tuple in R and y
is a tuple in S
R and S need not be union compatible
 Each row of S1 is paired with each row of R1.
 Result schema has one field per field of S1 and R1, with
field names `inherited’ if possible.
Cartesian (Cross) Product

R1 sid bid day S1 sid sname rating age


22 101 10/10/96 22 dustin 7 45.0
58 103 11/12/96 31 lubber 8 55.5
58 rusty 10 35.0
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 22 101 10/10/96
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0 22 101 10/10/96
58 rusty 10 35.0 58 103 11/12/96
R1S1
Joins
 Condition Join: R  S   ( R  S)
c c
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 58 103 11/12/96
S1  R1
S1. sid  R1. sid
 Result schema same as that of cross-product.
 Fewer tuples than cross-product. Filters tuples not
satisfying the join condition.
 Sometimes called a theta-join.
Joins
 Equi-Join: A special case of condition join where the condition c contains
only equalities.

sid sname rating age bid day


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

 Result schema similar to cross-product, but only one copy of fields for which
equality is specified.
 Natural Join: Equijoin on all common fields.

sid ,.., age,bid ,..(S1 sid R1)


Division
 Not supported as a primitive operator, but useful for expressing queries
like:
Find sailors who have reserved all boats.
 Precondition: in A÷B, the attributes in B must be included in the schema
for A. Also, the result has attributes A-B.
 SALES(supId, prodId);
 PRODUCTS(prodId);
 Relations SALES and PRODUCTS must be built using projections.

 SALES÷PRODUCTS: the ids of the suppliers supplying ALL products.


Examples of Division A÷B

sno pno pno pno pno


s1 p1 p2 p2 p1
s1 p2 p4 p2
B1
s1 p3 B2 p4
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
Expressing A÷B Using Basic Operators

 Division is not essential op; just a useful shorthand.


 (Division is NOT implemented in SQL).
 For SALES÷PRODUCTS, compute all products such that there exists at
least one supplier not supplying it.
 r ÷ s is defined as Π RS(r) − Π RS((Π RS(r) × s) − Π RS,S(r))

A (( (Sales)Pr oducts) Sales)


sid sid (sid , pid )

The answer is sid(Sales) - A


Find names of sailors who’ve reserved boat
#103
 Solution 1:

 sname(( Reserves)  Sailors)


bid 103
 Solution 2:
 (Temp1,  Re serves)
bid  103
 ( Temp2, Temp1  Sailors)
 sname (Temp2)
 Solution 3:
 sname ( (Re serves  Sailors))
bid 103
Find names of sailors who’ve reserved a red
boat

 Information about boat color only available in


Boats; so need an extra join:

 sname (( Boats)  Re serves  Sailors)


color ' red '

 A more efficient solution:


 sname ( ((  Boats)  Re s)  Sailors)
sid bid color ' red '

A query optimizer can find this, given the first solution!


Find sailors who’ve reserved a red or a green
boat
 Can identify all red or green boats, then find sailors who’ve
reserved one of these boats:

 (Tempboats, ( Boats))
color ' red '  color ' green '
 sname(Tempboats  Re serves  Sailors)

 What happens if  is replaced by  in this query?


Find sailors who’ve reserved a red and a
green boat
 Previous approach won’t work! Must identify sailors
who’ve reserved red boats, sailors who’ve reserved
green boats, then find the intersection (note that sid is
a key for Sailors):

 (Tempred,  (( Boats)  Re serves))


sid color ' red '
 (Tempgreen,  (( Boats)  Re serves))
sid color ' green'

 sname((Tempred  Tempgreen)  Sailors)


Find the names of sailors who’ve reserved all
boats

 Uses division; schemas of the input relations to /


must be carefully chosen:

 (Tempsids, ( Re serves) / ( Boats))


sid, bid bid
 sname (Tempsids  Sailors)

 To find sailors who’ve reserved all ‘Interlake’ boats:


..... / ( Boats)
bid bname ' Interlake'
Summary

 The relational model has rigorously


defined query languages that are simple
and powerful.
 Relational algebra is more operational;
useful as internal representation for
query evaluation plans.
 Several ways of expressing a given query;
a query optimizer should choose the most
efficient version.

You might also like