Chapter 6
Chapter 6
Two mathematical Query Languages form the basis for Relational Query
Languages
Relational Algebra:
Relational Calculus:
Relational Algebra
The basic set of operations for the relational model is known as the relational
algebra. These operations enable a user to specify basic retrieval requests.
The result of the retrieval is a new relation, which may have been formed
from one or more relations. The algebra operations thus produce new
relations, which can be further manipulated using operations of the same
algebra.
Employee
EmpI FNam LNam SkillI Skill SkillType Scho SchoolA SkillLev
D e e D ol dd el
12 Abebe Mekuri 2 SQL Database AAU Sidist_Kilo 5
a
16 Lemm Alemu 5 C++ Programmin Unity Gerji 6
a g
28 Chane Kebede 2 SQL Database AAU Sidist_Kilo 10
25 Abera Taye 6 VB6 Programmin Helic Piazza 8
g o
65 Almaz Belay 2 SQL Database Helic Piazza 9
o
24 Dereje Tamiru 8 Oracl Database Unity Gerji 5
e
51 Selam Belay 4 Prolo Programmin Jimm Jimma 8
g g a City
94 Alem Kebede 3 Cisco Networking AAU Sidist_Kilo 7
18 Girma Dereje 1 IP Programmin Jimm Jimma 4
g a City
13 Yared Gizaw 7 Java Programmin AAU Sidist_Kilo 6
g
1. Selection
Selects subset of tuples/rows in a relation that satisfy selection
condition.
Selection operation is a unary operator (it is applied to a single
relation)
The Selection operation is applied to each tuple individually
The degree of the resulting relation is the same as the original relation
but the cardinality (no. of tuples) is less than or equal to the original
relation.
The Selection operator is commutative.
Set of conditions can be combined using Boolean operations ( (AND),
(OR), and ~(NOT))
No duplicates in result!
Schema of result identical to schema of (only) input relation.
Result relation can be the input for another relational algebra
operation! (Operator composition.)
It is a filter that keeps only those tuples that satisfy a qualifying
condition (those satisfying the condition are selected while others are
discarded.)
Notation:
<Selection Condition> <Relation Name>
Example: Find all Employees with skill type of Database.
If the query is all employees with a SkillType Database and School Unity the
relational algebra operation and the resulting relation will be as follows.
< SkillType =”Database” AND School=”Unity”> (Employee)
EmpI FNa LNam SkillI Skill SkillTyp Scho SchoolA SkillLev
D me e D e ol dd el
24 Derej Tamir 8 Oracl Databas Unity Gerji 5
e u e e
2. Projection
Selects certain attributes while discarding the other from the base
relation.
The PROJECT creates a vertical partitioning – one with the needed
columns (attributes) containing results of the operation and other
containing the discarded Columns.
Deletes attributes that are not in projection list.
Schema of result contains exactly the fields in the projection list, with
the same names that they had in the (only) input relation.
Projection operator has to eliminate duplicates!
Note: real systems typically don’t do duplicate elimination unless
the user explicitly asks for it.
If the Primary Key is in the projection list, then duplication will not
occur
Duplication removal is necessary to insure that the resulting table is
also a relation.
Notation:
<Selected Attributes> <Relation Name>
Example: To display Name, Skill, and Skill Level of an employee, the query
and the resulting relation will be:
If we want to have the Name, Skill, and Skill Level of an employee with salary
greater than 1500 and working for department 5, we can write the
expression for this query using the two alternatives:
Then Result will be equivalent with the relation we get using the
first alternative.
4. Set Operations
The three main set operations are the Union, Intersection and Set Difference.
The properties of these set operations are similar with the concept we have
in mathematical set theory. The difference is that, in database context, the
elements of each set, which is a Relation in Database, will be tuples. The set
operations are Binary operations which demand the two operand Relations to
have type compatibility feature.
Type Compatibility
Two relations R1 and R2 are said to be Type Compatible if:
1. The operand relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) have the
same number of attributes, and
2. The domains of corresponding attributes must be compatible; that
is, Dom(Ai)=Dom(Bi) for i=1, 2, ..., n.
To illustrate the three set operations, we will make use of the following two
tables:
Employee
EmpI FNam LNam SkillI Skill SkillType Scho SkillLev
D e e D ol el
12 Abebe Mekuria 2 SQL Database AAU 5
16 Lemm Alemu 5 C++ Programmin Unity 6
a g
28 Chane Kebede 2 SQL Database AAU 10
25 Abera Taye 6 VB6 Programmin Helico 8
g
65 Almaz Belay 2 SQL Database Helico 9
24 Dereje Tamiru 8 Oracl Database Unity 5
e
51 Selam Belay 4 Prolo Programmin Jimma 8
g g
94 Alem Kebede 3 Cisco Networking AAU 7
18 Girma Dereje 1 IP Programmin Jimma 4
g
13 Yared Gizaw 7 Java Programmin AAU 6
g
a. UNION Operation
The result of this operation, denoted by R U S, is a relation that
includes all tuples that are either in R or in S or in both R and S.
Duplicate tuple is eliminated.
The two operands must be "type compatible"
b. INTERSECTION Operation
The result of this operation, denoted by R ∩ S, is a relation that
includes all tuples that are in both R and S. The two operands
must be "type compatible"
Eg: RelationOne ∩ RelationTwo
Employees who attend Database Course at AAU
The resulting relation for; R1 R2, R1 R2, or R1-R2 has the same
attribute names as the first operand relation R1 (by convention).
Example:
Employee
ID FName LName
123 Abebe Lemma
567 Belay Taye
822 Kefle Kebede
Dept
DeptID DeptNam MangI
e D
2 Finance 567
3 Personnel 123
Then the Cartesian product between Employee and Dept relations will be of
the form:
Employee X Dept:
ID FName LName DeptI DeptNam MangI
D e D
123 Abebe Lemma 2 Finance 567
123 Abebe Lemma 3 Personnel 123
567 Belay Taye 2 Finance 567
567 Belay Taye 3 Personnel 123
822 Kefle Kebede 2 Finance 567
822 Kefle Kebede 3 Personnel 123
6. JOIN Operation
The sequence of Cartesian product followed by select is used quite
commonly to identify and select related tuples from two relations, a special
operation, called JOIN. Thus in JOIN operation, the Cartesian Operation and
the Selection Operations are used together.
JOIN Operation is denoted by a symbol.
This operation is very important for any relational database with more than a
single relation, because it allows us to process relationships among relations.
The general form of a join operation on two relations
R(A1, A2,. . ., An) and S(B1, B2, . . ., Bm) is:
Where, R and S can be any relation that results from general relational
algebra expressions.
Since JOIN is an operation that needs two relation, it is a Binary operation.
Example:
Thus in the above example we want to extract employee information
about managers of the departments, the algebra query using the JOIN
operation will be.