CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
Additional Relational Algebra Operations
1. Additional Relational-Algebra Operations
The fundamental operations of the relational algebra are sufficient to express any relational-algebra query.
However, if we restrict ourselves to just the fundamental operations, certain common queries are lengthy
to express. Therefore, we define additional operations that do not add any power to the algebra, but simplify
common queries.
set-intersection (binary)
natural join (binary)
division (binary)
assignment (unary)
Figure 1. The loan relation. Figure: The depositor relation.
Figure 2. The borrower relation.
The Set-Intersection Operation
The first additional-relational algebra operation that we shall define is set intersection (∩). Suppose that
we wish to find all customers who have both a loan and an account. Using set intersection, we can write
Figure 3. Customers with both an account and a loan at the bank.
2. The Natural-Join Operation
It is often desirable to simplify certain queries that require Cartesian product operation. Natural join
operation helps in this regard.
The natural join is a binary operation that allows us to combine certain selection and a Cartesian product
into one operation. It is denoted by the “join” symbol |X|.
The natural join operation forms:
A Cartesian product of two arguments
Performs a selection forcing equality on those attributes that appear in both relation schemas and
finally
Removes duplicate attributes
As an illustration, consider again the example “Find the names of all customers who have a loan at the
bank, and find the amount of the loan.” We express this query by using the natural join as follows:
Division Operator (÷)
Division operator A÷B can be applied if and only if:
Attributes of B is proper subset of Attributes of A.
The relation returned by division operator will have attributes = (All attributes of A – All Attributes of
B)
The relation returned by division operator will return those tuples from relation A which are associated
to every B’s tuple.
Table 1
STUDENT_SPORTS
ROLL_NO SPORTS
1 Badminton
2 Cricket
2 Badminton
4 Badminton
3. Table 2
ALL_SPORTS
SPORTS
Badminton
Cricket
Consider the relation STUDENT_SPORTS and ALL_SPORTS given in Table 2 and Table 3 above.
To apply division operator as
STUDENT_SPORTS÷ ALL_SPORTS
The operation is valid as attributes in ALL_SPORTS is a proper subset of attributes in
STUDENT_SPORTS.
The attributes in resulting relation will have attributes {ROLL_NO,SPORTS}-{SPORTS}=ROLL_NO
The tuples in resulting relation will have those ROLL_NO which are associated with all B’s tuple
{Badminton, Cricket}. ROLL_NO 1 and 4 are associated to Badminton only. ROLL_NO 2 is
associated to all tuples of B. So the resulting relation will be:
ROLL_NO
2
The Assignment Operation
It is convenient at times to write a relational-algebra expression by assigning parts of it to temporary relation
variables. The assignment operation, denoted by ←, works like assignment in a programming language.
Extended Relational-Algebra Operations
Generalized Projection
The generalized-projection operation extends the projection operation by allowing arithmetic functions to
be used in the projection list. The generalized projection operation has the form
ΠF1,F2,...,Fn (E)
where E is any relational-algebra expression, and each of F1, F2, . . . , Fn is an arithmetic expression
involving constants and attributes in the schema of E.
Aggregate Functions
Aggregate functions take a collection of values and return a single value as a result.
For example, the aggregate function sum takes a collection of values and returns the sum of the values.
The aggregate function avg returns the average of the values. The aggregate function count returns the
number of the elements in the collection Other common aggregate functions include min and max, which
return the minimum and maximum values in a collection
4. Suppose that we want to find out the total sum of salaries of all the part-time employees in the bank. The
relational-algebra expression for this query is:
There are cases where we must eliminate multiple occurrences of a value before computing an aggregate
function. If we do want to eliminate duplicates, we use the same function names as before, with the addition
of the hyphenated string “distinct” appended to the end of the function name (for example, count-distinct).
An example arises in the query “Find the number of branches appearing in the pt-works relation.” We write
this query as follows:
Suppose we want to find the total salary sum of all part-time employees at each branch of the bank
separately, rather than the sum for the entire bank.
The following expression using the aggregation operator achieves the desired result:
if we want to find the maximum salary for part-time employees at each branch, in addition to the sum of the
salaries, we write the expression
We can apply a rename operation to the result in order to give it a name. As a notational convenience,
attributes of an aggregation operation can be renamed as illustrated below:
Outer Join
Join operation is essentially a cartesian product followed by a selection criterion.
The outer-join operation is an extension of the join operation to deal with missing information.
There are actually three forms of the operation: left outer join, denoted ; right outer join, denoted
; and full outer join, denoted .
5. Left Outer Join(A B)
In the left outer join, operation allows keeping all tuple in the left relation. However, if there is no matching
tuple is found in right relation, then the attributes of right relation in the join result are filled with null values.
Consider the following 2 Tables
A
Num Square
2 4
3 9
4 16
A B
Num Square Cube
2 4 8
3 9 18
4 16 null
Right Outer Join: ( A B )
In the right outer join, operation allows keeping all tuple in the right relation. However, if there is no matching
tuple is found in the left relation, then the attributes of the left relation in the join result are filled with null
values.
B
Num Cube
2 8
3 18
5 75
6. A B
Num Cube Square
2 8 4
3 18 9
5 75 null
Full Outer Join: ( A B)
In a full outer join, all tuples from both relations are included in the result, irrespective of the matching
condition.
A B
Num Square Cube
2 4 8
3 9 18
4 16 null
5 null 75
A. S. M. Shafi
Lecturer
Department of Computer Science and Engineering
Khwaja Yunus Ali University
Enaytpur, Sirajgonj-6751, Bangladesh