0% found this document useful (0 votes)
7 views19 pages

LECTURE 12 (Examples On Relational Algebra)

The document provides an overview of relational algebra operations, including SELECT, PROJECT, UNION, INTERSECTION, and JOIN, along with their properties and examples. It explains how these operations manipulate relations in a database, such as selecting specific tuples, projecting columns, and combining relations. Additionally, it highlights the importance of operations like EQUIJOIN and the use of aggregate functions in querying databases.

Uploaded by

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

LECTURE 12 (Examples On Relational Algebra)

The document provides an overview of relational algebra operations, including SELECT, PROJECT, UNION, INTERSECTION, and JOIN, along with their properties and examples. It explains how these operations manipulate relations in a database, such as selecting specific tuples, projecting columns, and combining relations. Additionally, it highlights the importance of operations like EQUIJOIN and the use of aggregate functions in querying databases.

Uploaded by

Poonam Bhardwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

EXAMPLES ON

RELATIONAL
ALGEBRA

Author: Poonam Sharma


Author: Poonam Sharma
Author: Poonam Sharma
Author: Poonam Sharma
SELECT (Unary operation)
 SELECT operation is used to select a subset of the tuples from a relation that satisfy a
selection condition.

SELECT Operation Properties

◦ The SELECT operation  <selection condition>(R) produces a relation S


that has the same schema as R

◦ The SELECT operation  is commutative; i.e.,


 <condition1>( < condition2> ( R)) =  <condition2> ( < condition1> ( R))

◦ A cascaded SELECT operation may be applied in any order; i.e.,


 <condition1>( < condition2> ( <condition3> ( R))
=  <condition2> ( < condition3> ( < condition1> ( R)))

◦ A cascaded SELECT operation may be replaced by a single selection


with a conjunction of all the conditions; i.e.,
 <condition1>( < condition2> ( <condition3> ( R))
=  <condition1> AND < condition2> AND < condition3> ( R)))

Author: Poonam Sharma


Example
Example: To select the EMPLOYEE tuples
whose department number is four or those
whose salary is greater than $30,000 the
following notation is used:
DNO = 4 (EMPLOYEE)
SALARY > 30,000 (EMPLOYEE)

Author: Poonam Sharma


PROJECT
This operation selects certain columns
from the table and discards the other
columns. The PROJECT creates a vertical
partitioning.
PROJECT Operation Properties
◦ The number of tuples in the result of
projection  <list> (R)is always less or equal to the
number of tuples in R.

◦ If the list of attributes includes a key of R,


then the number of tuples is equal to the number of
tuples in R.

Author: Poonam Sharma


EXAMPLE
To list each employee’s first and last name and
salary, the following is used:
LNAME,
FNAME,SALARY(EMPLOYEE)

Example: To retrieve the first name, last name,


and salary of all employees who work in
department number 5, we must apply a select
and a project operation. We can write a single
relational algebra expression as follows:

FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))


Author: Poonam Sharma
UNION
The result of this operation, denoted by R  S, is a relation
that includes all tuples that are either in R or in S or in
both R and S. Duplicate tuples are eliminated.
Example: To retrieve the social security numbers of all
employees who either work in department 5 or directly
supervise an employee who works in department 5, we can
use the union operation as follows:
DEP5_EMPS  DNO=5 (EMPLOYEE)
RESULT1   SSN(DEP5_EMPS)
RESULT2(SSN)   SUPERSSN(DEP5_EMPS)
RESULT  RESULT1  RESULT2
The union operation produces the tuples that are in either
RESULT1 or RESULT2 or both. The two operands must
be “type compatible”.
Author: Poonam Sharma
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"
Set Difference (or MINUS) Operation
The result of this operation, denoted by R -
S, is a relation that includes all tuples that
are in R but not in S. The two operands must
be "type compatible”.

Author: Poonam Sharma


Author: Poonam Sharma
Notice
that both union and intersection are
commutative operations; that is
R  S = S  R, and R  S = S  R
Both union and intersection can be treated as n-
ary operations applicable to any number of
relations as both are associative operations; that is
R  (S  T) = (R  S)  T, and (R  S)  T =
R  (S  T)

The minus operation is not commutative; that is,


in general
R-S≠S–R
Author: Poonam Sharma
CARTESIAN PRODUCT
◦ This operation is used to combine tuples
from two relations in a combinatorial
fashion. In general, the result of R(A1, A2, . .
., An) x S(B1, B2, . . ., Bm) is a relation Q with
degree n + m attributes Q(A1, A2, . . ., An,
B1, B2, . . ., Bm), in that order. The resulting
relation Q has one tuple for each
combination of tuples—one from R and one
from S.
◦ Hence, if R has nR tuples (denoted as |R| =
nR ), and S has nS tuples, then
| R x S | will have nR * nS tuples.
◦ The two operands do NOT have to be "type
compatible”

Author: Poonam Sharma


JOIN
◦ 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. It is denoted by a
◦ 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:
R <join condition> S
Author: Poonam Sharma
EQUIJOIN Operation


The most common use of join involves join
conditions with equality comparisons only. Such a join,
where the only comparison operator used is =, is called
an EQUIJOIN. In the result of an EQUIJOIN
• we always have one or more pairs of attributes (whose
names need not be identical) that have identical values
in every tuple.

Author: Poonam Sharma


Example: Suppose that we want to retrieve the name of the manager
of each department. To get the manager’s name, we need to
combine each DEPARTMENT tuple with the EMPLOYEE tuple
whose SSN value matches the MGRSSN value in the department
tuple. We do this by using the join operation.
DEPT_MGR  DEPARTMENT MGRSSN=SSN
EMPLOYEE

Author: Poonam Sharma


DIVISION

Author: Poonam Sharma


AGGREGATE AND GROUP
BY

Author: Poonam Sharma


EXAMPLES
 Q1: Retrieve the name and address of all
employees who work for the ‘Research’
department.
RESEARCH_DEPT   DNAME=’Research’ (DEPARTMENT)
RESEARCH_EMPS  (RESEARCH_DEPT DNUMBER=

EMPLOYEE)
DNOEMPLOYEE

RESULT   FNAME, LNAME, ADDRESS (RESEARCH_EMPS)


 Q6: Retrieve the names of employees who
have no dependents.
ALL_EMPS   SSN(EMPLOYEE)
EMPS_WITH_DEPS(SSN)   ESSN(DEPENDENT)
EMPS_WITHOUT_DEPS  (ALL_EMPS - EMPS_WITH_DEPS)
RESULT   LNAME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)
Author: Poonam Sharma

You might also like