You are on page 1of 18

RELATIONAL ALGEBRA

Relational Algebra can be defined as a set of operators used to manipulate relations


(tables). Each operator of the relational algebra takes either one or two relations as its
input and produces a new relation as its output. It is a procedural language that can be
used to tell the DBMS how to build a new relation from one or more relations in the
database. In relational algebra, user has to specify what information is required from the
database and what are the procedures or steps performed on the database to obtain the
required information.
Types of Relational Operators:-

Relational operators can be broadly divided into two


types:
(1) Traditional Set Operators
(2) Special Operators
The traditional set operators are:-

(i) Union
(ii) Difference
(iii) Intersection
(iv) Cartesian Product
Union:-
The union operation of two relations (tables) results in a new relation containing
rows from both relations with duplicates removed. The two relations used in the
union operation must be union compatible i.e. they must have the same number of
columns and corresponding columns in the select statement must come from the
same domain. Union operation is a binary operation which means that it always
operates on two relations. The notation for the union of relations (tables) P and Q is
PᴗQ.
Suppose we have two relations P and Q, then the union of P and Q is R as shown
below: Here relations P and Q are having same degree 2 and they are drawn from
common domain so they are union compatible. The resultant relation R obtained
by taking union of P and Q, contains all the rows from P as well as from Q but the
duplicate rows such as (503, Anand}, {504, Amitabh}, (510, Kapil) occur only once.
Thus duplicacy is removed.
Intersection:-

The Intersection of two relations (tables) results in a new table containing


rows that are common to both relations. The two relations used in
Intersection operation must be union compatible. The Intersection operation
is a binary operation and the notation for the intersection of tworelations P
and Q is PᴖQ.
Suppose two relations P and Q are given and the resultant
relation obtained on intersection is shown by R.

The resultant relation R obtained by taking intersection of


P and Q, contains all the rows which are common to both
the table.
Difference:-

The difference of two relations (tables) results in a new relation


that contains those tuples (rows) that occur in the first relation
but not in the second relation. The two relations in the difference
operation must be union compatible. The difference operation is
a binary operation which means that it always operates on two
relations. The notation for the difference of two relations P and Q
is P-Q.
Suppose two relations P and Q and the resultant relation obtained on
taking their differenceshown by R.
The resultant relation R obtained by taking difference of two relations
P and Q where P is the first relation and Q is the second relation.
Relation R contains all the rows that are contained in relation P but
not in relation Q. For example (503, Anand) is contained both in P
and Q tables so it is not a part of resultant relation R.
Cartesian Product:-

The Cartesian product of two relations (tables) results in a new relation that
includes concatenation or combination of every tuple (row) of first relation with
every tuple (row) of second relation, In other words, the new relation is created
consisting of all possible combination of the tuples (rows). It is also known as
cross product or cross join. It is not necessary for the relation on which this
operation is to be performed to be union compatible. The Cartesian product is a
binary operation which means that it always operates on two relations. The
notation for the Cartesian product of relations (tables) P and Q is PX Q.
Suppose we are given two relations P and Q and the resultant relation obtained on
performing cartesian product is shown by R (i.e. R = PxQ) The resultant relation R
obtained by calculating the cartesian product of relations P and Q, contains the
possible combination of all the rows of P relation (table) with the rows of the Q relation
(table). The total number of the columns in the resultant relation R is equal to the sum
of a number of columns in relation P and relation Q i.e. (2+2=4). The total number of
rows in the resultant relation (table) R is equal to the product of number of rows in
relation P and relation Q. i.e. (3x2=6).
(2) Special Operators:-

The special relational operators are:


(i) Selection (Unary Operation)
(ii) Projection (Unary Operation)
(iii) Join
(iv) Division
Selection:-

The selection operation also known as the restriction operation


results in a new relation (table) that contains only those rows of
relation that satisfy a specified condition. It is a unary operation
which means that it can work only on a single relation. The
selection operation is denoted by lowercase Greek Sigma (0) with
a predicate appearing as a subscript and relation name is given in
parenthesis following the sigma.
σ <select condition> (<relation>)
Now suppose we have to find all employees having salary 28000 in the relation. The selection operation is denoted
as Salary28000 (EMPLOYEE) and the result of the selection or restriction which is applied on EMPLOYEE relation
satisfying the condition (salary 28000) is shown. It contains all the tuples (rows) from the EMPLOYEE relation (table)
where salary of the employee is greater than or equal to 8000In addition to the greater than equal to (2)
comparison operator, the selection operation can also use other comparison operators such as greater than (>),
less than (<), less than equal to (S), not equal to (#), equal to (=), greater than equal to (2), and the logical operators
such as AND, OR and NOT. In the selection operation, the condition (salary 28000) is applied independently to
every row of the EMPLOYEE relation. This is done by substituting each occurrence of the attribute specified in the
selection's condition (i.e. salary) with its values (10000, 6000, 7000, 8000, 15000, 12000) in each row of the table. If
the condition is true then the row is selected otherwise it is rejected.
Projection:-
The Projection operation results in a new relation (table) that contains a subset of
columns of a relation (table) and eliminates any duplicate rows that may result. It is
a unary operation which means that it operates only on a single relation (table).The
Projection operation is denoted by a Greek capital letter PI (II) and the attribute(s),
to be retrieved appear as subscripts separated by commas and relation (table)
name is given in parentheses following the PI.

σ <attribute list> (<relation>)


Suppose an EMPLOYEE relation (table) having the attributes, Id,
Name and Salary where Id is the primary key and we want a list of
name of employees in the EMPLOYEE relation shown in Fig. (B).
Also suppose that we want a list of name and salary details of each
employee in EMPLOYEE relation
Division:-

The division operation results in a new relation (table) such that every tuple (row)
appearing in the resultant relation must exist in the dividend relation (i.e. First table) in
combination with the every tuple (row) in divisor relation (second table). It is a binary
operation which operates on two relations. The division is denoted by a symbol "÷".
The notation for the division of two relations P and Q is P+ Q where P is the dividend
relation and the Q is the divisor relation. The division operation is suited to queries that
include the phrase "For all".
Consider another example, in which we take two relations Pand Q.

The resultant relation R obtained by P÷Q in each case when tuples of Q are differentWhen a relation
P is divided by a relation Q (ie P÷ Q) then result is all the tuples that appear the divided relation i.e. P
in combination with divisor relation i.e. Q.In case (1) where Q is having values {n,, n} and P is having
some values that use n, and n in addition to other values. These are {m,n; mn; m.n; mn: mn}. Out of
these values, all except man, is having fixed values of m, where i = 1,4 with n, and n So on dividing P
with Q relation i.e. P÷ Q, it results in {m,, m.}. Similarly in case (ii) where Q is having value {n}. So on
performing P+Q, the answer is {m,, m₂, m₂). It can also be written as
m₁n/n,= m₂; m₂; n/n, = m₂; m₂ n/n, = m₂

You might also like