New Session 13
New Session 13
1
Agenda:
Binary Relational operations
Additional relational operations
2
Binary Relational Operations
• 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.
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
where R and S can be any relations that result from general relational algebra
expressions.
Binary Relational Operations (cont.)
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
Binary Relational Operations (cont.)
• 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.
The JOIN seen in the previous example was EQUIJOIN.
Because one of each pair of attributes with identical values is superfluous, a new operation called natural join—
denoted by *—was created to get rid of the second (superfluous) attribute in an EQUIJOIN condition.
The standard definition of natural join requires that the two join attributes, or each pair of corresponding join
attributes, have the same name in both relations. If this is not the case, a renaming operation is applied first.
Binary Relational Operations (cont.)
Example: To apply a natural join on the DNUMBER attributes of DEPARTMENT and
DEPT_LOCATIONS, it is sufficient to write:
DEPT_LOCS DEPARTMENT * DEPT_LOCATIONS
Complete Set of Relational Operations
• For example:
R S ≡(R S ) – ((R - S) (S - R))
R <join condition> S≡ <join condition> (R X S)
Binary Relational Operations (cont.)
• DIVISION Operation
• The division operation is applied to two relations
R(Z) S(X), where X subset Z. Let Y = Z - X (and hence Z = X Y); that is, let Y be
the set of attributes of R that are not attributes of S.
• The result of DIVISION is a relation T(Y) that includes a tuple t if tuples tR appear in
R with tR [Y] = t, and with
tR [X] = ts for every tuple ts in S.
• For a tuple t to appear in the result T of the DIVISION, the values in t must appear
in R in combination with every tuple in S.
Binary Relational Operations (cont.)
Recap of Relational Algebra Operations
Additional Relational Operations
• A type of request that cannot be expressed in the basic relational algebra is to specify mathematical
aggregate functions on collections of values from the database.
• Examples of such functions include retrieving the average or total salary of all employees or the
total number of employee tuples. These functions are used in simple statistical queries that
summarize information from the database tuples.
• Common functions applied to collections of numeric values include SUM, AVERAGE, MAXIMUM,
and MINIMUM. The COUNT function is used for counting tuples or values.
Additional Relational Operations (cont.)
Additional Relational Operations (cont.)
ℱMAX Salary (Employee) retrieves the maximum salary value from the Employee relation
ℱMIN Salary (Employee) retrieves the minimum Salary value from the Employee relation
ℱSUM Salary (Employee) retrieves the sum of the Salary from the Employee relation
DNOℱCOUNT SSN, AVERAGE Salary (Employee) groups employees by DNO (department number) and
computes the count of employees and average salary per department.[ Note: count
just counts the number of rows, without removing duplicates]
Additional Relational Operations (cont.)
• Recursive Closure Operations
• Another type of operation that, in general, cannot be specified in the basic original relational
algebra is recursive closure. This operation is applied to a recursive relationship.
• An example of a recursive operation is to retrieve all SUPERVISEES of an EMPLOYEE e at all levels—
that is, all EMPLOYEE e’ directly supervised by e; all employees e’’ directly supervised by each
employee e’; all employees e’’’ directly supervised by each employee e’’; and so on .
• Although it is possible to retrieve employees at each level and then take their union, we cannot, in
general, specify a query such as “retrieve the supervisees of ‘James Borg’ at all levels” without
utilizing a looping mechanism.
• The SQL3 standard includes syntax for recursive closure.
Additional Relational Operations (cont.)
Additional Relational Operations (cont.)
• The OUTER JOIN Operation
• In NATURAL JOIN tuples without a matching (or related) tuple are eliminated from the join result. Tuples with
null in the join attributes are also eliminated. This amounts to loss of information.
• A set of operations, called outer joins, can be used when we want to keep all the tuples in R, or all those in S,
or all those in both relations in the result of the join, regardless of whether or not they have matching tuples in
the other relation.
• The left outer join operation keeps every tuple in the first or left relation R in R S; if no matching tuple is
found in S, then the attributes of S in the join result are filled or “padded” with null values.
• A similar operation, right outer join, keeps every tuple in the second or right relation S in the result of R
S.
• A third operation, full outer join, denoted by keeps all tuples in both the left and the right relations
when no matching tuples are found, padding them with null values as needed.
Additional Relational Operations (cont.)
Additional Relational Operations (cont.)
• OUTER UNION Operations
• The outer union operation was developed to take the union of tuples from two relations if the relations are not
union compatible.
• This operation will take the union of tuples in two relations R(X, Y) and S(X, Z) that are partially compatible,
meaning that only some of their attributes, say X, are union compatible.
• The attributes that are union compatible are represented only once in the result, and those attributes that are
not union compatible from either relation are also kept in the result relation T(X, Y, Z).
• Example: An outer union can be applied to two relations whose schemas are STUDENT(Name, SSN, Department,
Advisor) and INSTRUCTOR(Name, SSN, Department, Rank). Tuples from the two relations are matched based on
having the same combination of values of the shared attributes—Name, SSN, Department. If a student is also an
instructor, both Advisor and Rank will have a value; otherwise, one of these two attributes will be null.
The result relation STUDENT_OR_INSTRUCTOR will have the following attributes:
STUDENT_OR_INSTRUCTOR (Name, SSN, Department, Advisor, Rank)
Examples of Queries in Relational Algebra
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= DNOEMPLOYEEEMPLOYEE)
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)
© 2017-18 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 20
© 2017-18 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 21
© 2017-18 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 22
© 2017-18 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 23
© 2017-18 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 24
© 2017-18 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 25
© 2017-18 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 26
© 2017-18 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 27
© 2017-18 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 28