Database Management System
CBCS BHCS10 Parul Chachra
Chapter 8
The Rela=onal Algebra and Rela=onal Calculus
Reference: Book prescribed as per the syllabus
DIVISION Opera=on
• The division opera=on is applied to two rela=ons
• 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 aUributes of R that are not
aUributes of S
• The result of DIVISION is a rela=on 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 tx in S.
• For a tuple t to appear in the result T of the DIVISION, the values in t must appear in R in combina=on with every tuple in S.
DIVISION Opera=on
• Example of Division opera=on:
• (a) shows Dividing SSN_PNOS by SMITH_PNOS
• (b) shows T ← R ÷ S.
Aggregate Func=ons and Grouping
• A type of request that cannot be expressed in the basic rela=onal algebra is to specify
mathema=cal aggregate func=ons on collec=ons of values from the database.
• Examples of such func=ons include retrieving the average or total salary of all employees or the total number of employee tuples.
• The COUNT func=on is used for coun=ng tuples or values.
• Common func=ons applied to collec=ons of numeric values include SUM, AVERAGE,
MAXIMUM, and MINIMUM
Aggregate Func=ons and Grouping
• Use of the Aggregate Func=onal opera=on
• MAX Salary (EMPLOYEE) retrieves the maximum salary value from the EMPLOYEE rela=on
• MIN Salary (EMPLOYEE) retrieves the minimum Salary value from the EMPLOYEE rela=on
• COUNT SSN, AVERAGE Salary (EMPLOYEE) computes the count (number) of employees and their average salary
• Note: count just counts the number of rows, without removing duplicates
Aggregate Func=ons and Grouping
• Grouping can be combined with Aggregate Func=ons
• Example: For each department, retrieve the DNO, COUNT SSN, and AVERAGE SALARY
• DNO COUNT SSN, AVERAGE Salary (EMPLOYEE)
• Above opera=on groups employees by DNO (department number) and computes the
count of employees and average salary per department
Aggregate Func=ons and Grouping
• Example of Aggregate Func=ons and grouping:
Recursive Closure Opera=ons
• Another type of opera=on that, in general,
cannot be specified in the basic original rela=onal algebra is recursive closure.
• This opera=on is applied to a recursive
rela?onship between tuples of the same type, such as the rela=onship between an employee and a supervisor.
• An example of a recursive opera=on 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.
Recursive Closure Opera=ons
• For example, to specify the Ssns of all employees eʹ directly supervised—at level one—by the employee e whose name is
‘James Borg’, we can apply the following opera=on:
• To retrieve all employees supervised by Borg at level 2—that is, all employees eʹʹ supervised by some employee eʹ who is directly supervised by Borg—we can apply another JOIN to the result of the first query, as follows:
• To get both sets of employees supervised at levels 1 and 2 by
‘James Borg’, we can apply the UNION opera=on to the two results, as follows: