Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 106

Database Design

IS222

Dr. Surbhi Bhatia


sbhatia@kfu.edu.sa
Chapter 6
The Relational Algebra and Relational Calculus

DATA MANIPULATION
LANGUAGES
Chapter 6 Outline

 Unary Relational Operations: SELECT and


PROJECT
 Relational Algebra Operations from Set
Theory
 Binary Relational Operations: JOIN and
DIVISION
 Additional Relational Operations
The Relational Algebra and
Relational Calculus
 Understanding Algebra & Calculus is key to
understanding SQL, query processing

 Provide basis for operation of SQL


The Relational Algebra and
Relational Calculus
 Relational algebra
– Basic set of operations for the relational model
 Relational algebra expression
– Sequence of relational algebra operations
 Relational calculus
– Higher-level declarative language for
specifying relational queries
!
RDBMS Architecture

How does a SQL engine work ?

Relational
SQL Optimized
Algebra Execution
Query RA Plan
(RA) Plan

Declarative Translate to Find logically Execute each


query (from relational equivalent- but operator of the
user) algebra more efficient- optimized plan!
expression RA expression
RDBMS Architecture

How does a SQL engine work ?

Relational
SQL Optimized
Algebra Execution
Query RA Plan
(RA) Plan

Relational Algebra allows us to translate declarative


(SQL) queries into precise and optimizable
expressions!
Relational Algebra

 Relational algebra operations work on one or more


relations to define another relation leaving the original
intact

 Both operands and results are relations, so output from


one operation can become input to another operation

 Allows expressions to be nested, just as in arithmetic.


This property is called closure.
Five Basic Operators
 Unary Operators
1. Select
2. Project
3. Rename
 Binary Operators
3. Union
4. Set Difference
5. Cartesian Product
 Also Join, Intersection, and Division
operations, which can be expressed in terms
of 5 basic operations.
SELECT
 The SELECT operation is used to select a subset of the
tuples from a relation based on a selection condition

– The selection condition acts as a filter


– Keeps only those tuples that satisfy the qualifying
condition
– Tuples satisfying the condition are selected whereas the
other tuples are discarded (filtered out)
Students(sid,sname,gpa)

SQL:
 Returns all tuples which
SELECT *
satisfy a condition FROM Students
 Notation: c(R) WHERE gpa > 3.5;

 Examples
– Salary > 40000 (Employee)
RA:
– name = “Smith” (Employee)
 The condition c can be
=, <, , >, , <>
Select
 To process a selection,
– Look at each tuple
– See if we have a match (based on the condition)
 The Degree of the resulting relation is the same
as the degree of the relation i.e resultant relation
as same number of attribute as base relation
 σ is Commutative: σc1 (σc2(R)) = σc2 (σc1(R))
σsalary>30000 (σDno=5(R)) = σDno=5 (σsalary>30000 (R))
Selection Example

stId stName stAdr prName curSem


S1020 Sohail Dar F/8 MCS 4
S1038 Shoaib Ali H#23 BCS 3
S1015 Tahira Ejaz H#99 MCS 5
S1018 Arif Zia H#10 BIT 5

1. σ Curr_Sem > 3 (STUDENT)

2. σ Studid = ‘S1038’ (STUDENT)


Selection Example
1. σ Curr_Sem > 3 (STUDENT)
stId stName stAdr prName curSem
S1020 Sohail Dar F/8 MCS 4
S1015 Tahira Ejaz H#99 MCS 5
S1018 Arif Zia H#10 BIT 5

2. σ Studid = ‘S1038’ (STUDENT)

stId stName stAdr prName curSem


S1038 Shoaib Ali H#23 BCS 3
SELECT: Example
 Select the tuples for all employees who either
work in department 4 and make over $25000
per year,
 or work in department 5 and make over
$30000
Examples of applying SELECT
operations

 (DNO=4 AND Salary>25000 ) OR (DNO=5 AND Salary>30000 )


(EMPLOYEE)
PROJECT
 If we are interested in only certain attributes of relation,
we use PROJECT

 This operation create vertical partition and keeps certain


columns (attributes) from a relation and discards the
other columns.
 Notation: PROJECT Operation is denoted by  (pi)

 a1, a2, ..., ak (R)


where a1 ... ak are attribute names and R is a
relation name
Students(sid,sname,gpa)

 Eliminates columns, then SQL:


removes duplicates SELECT DISTINCT
sname,
 Notation:  (R)
A1,…,An
gpa
 Example: project social- FROM Students;
security number and
names:
RA:
  SSN, Name (Employee)
– Output schema:
Answer(SSN, Name)
Projection Example
stId Name prgrm Sem Name prgrm

S10 Fatima BCS 4 Fatima BCS

S11 Fatima BCS 3 Tahira MCS

S12 Tahira MCS 5 Arif BIT

S13 Arif BIT 5

Duplicate rows eliminated


Name,prgrm (SUDENT)
Duplicate Elimination

 If the attribute list includes only non-key


attributes, duplicate tuples are likely to occur

 The PROJECT operation removes any


duplicate tuples
Examples of applying SELECT and
PROJECT operations
 Example: To list each employee’s first and last
name and salary, the following is used:
LNAME, FNAME,SALARY(EMPLOYEE)

 Example: (c) in figure, where (F,25000) is


repeating but resultant relation keeps only one
instance
Examples of applying SELECT and
PROJECT operations
Single expression versus
sequence of relational operations
 We may want to apply several relational algebra
operations one after the other

– Either we can write the operations as a single relational


algebra expression by nesting the operations, or

– We can apply one operation at a time and create


intermediate result relations.

In the latter case, we must give names to the relations that


hold the intermediate results using Assignment
Operator.
Assignment Operator
 Denoted by “:
 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))

 OR We can explicitly show the sequence of operations


using assignment, giving a name to each intermediate
relation:
– DEP5_EMPS   DNO=5(EMPLOYEE)
– RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)
RENAME
 The RENAME operator is denoted by  (rho)
 In some cases, we may want to rename the attributes of a
relation or the relation name or both
– Useful when a query requires multiple operations
 The general RENAME operation  can be
expressed by any of the following forms:
 S(R) changes: the relation name to S
 (B1, B2, …, Bn )(R) changes: the column (attribute) names to B1,
B1, …..Bn
 S (B1, B2, …, Bn )(R) changes both:
 the relation name to S, and
 the column (attribute) names to B1, B1, …..Bn
Rename Example
Temp ( DNO=5(EMPLOYEE))
R(Firstname, Last_name, Salary)(  FNAME, LNAME, SALARY (Temp))

 Resultant relation named R having collumn First_name, Last_name,


Salary
Relational Algebra Operations from
Set Theory
 Union

 Intersection

 Minus

 Cartesian Product
UNION Requirement

 It is a Binary operation, denoted by 

 The two operand relations R and S must be


“type compatible” (or UNION compatible)
 R and S must have exactly same number of attributes
(degree)

 Each pair of corresponding attributes must be type


compatible (have same or compatible domains)
UNION

– The result of 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
Since it is a set, there are no duplicate tuples

 Union is Commutative
RS=SR
Two relation has same number of column

Following is not allowed


Columns are not same

Following is ok, column


order change

Slide 1-30
Union Example
CS

CID ProgID Cred_Hrs CourseTitle


C2345 P1245 3 Operating Sytems
C3456 P1245 4 Database Systems
B4567 P9873 4 Business 1

Business

CID ProgID Cred_Hrs CourseTitle


B4567 P9873 4 Business 1
B8944 P4567 4 Financial Management
Union Example

CS  Business

CID ProgID Cred_Hrs CourseTitle


C2345 P1245 3 Operating Sytems
C3456 P1245 4 Database Systems
C4567 P9873 4 Business 1
B8944 P4567 4 Financial Management
UNION: Example

 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
UNION: Example

DEP5_EMPS  DNO=5 (EMPLOYEE)


RESULT1   SSN(DEP5_EMPS)
RESULT2  SUPERSSN(DEP5_EMPS)
RESULT  RESULT1  RESULT2

 The union operation produces the tuples that


are in either RESULT1 or RESULT2 or both
Query results refer to this database state

DEP5_EMPS

RESULT
RESULT1 RESULT2

Result of the UNION operation


RESULT ← RESULT1
RESULT2.
INTERSECTION
 INTERSECTION is denoted by 

 The result of the operation R  S, is a relation that


includes all tuples that are in both R and S
– The attribute names in the result will be the same as the
attribute names in R

 The two operand relations R and S must be “type


compatible”
Intersection Example

CS  Business
CID ProgID Cred_Hrs CourseTitle
C4567 P9873 4 Business 1
SET DIFFERENCE
 SET DIFFERENCE (also called MINUS or
EXCEPT) is denoted by –

 The result of R – S, is a relation that includes all


tuples that are in R but not in S
– The attribute names in the result will be the
same as the attribute names in R

 The two operand relations R and S must be


“type compatible”
Difference Example
CS-Business

CID ProgID Cred_Hrs CourseTitle


C2345 P1245 3 Operating Sytems
C3456 P1245 4 Database Systems
Example to illustrate the result of UNION,
INTERSECT, and DIFFERENCE
Some properties of UNION,
INTERSECT, and DIFFERENCE
 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
(R  S)  T = R  (S  T)
 The minus operation is not commutative; that is, in general
R–S≠S–R
CARTESIAN PRODUCT
 This operation is used to combine tuples from two
relations in a combinatorial fashion.

–The two operands needs not to be Type/union compatible. It


means they can be of different degree.

–It is denoted by X

–Suppose there is a relation R with attributes (A1, A2,...An)


and S with attributes (B1, B2……Bn). The Cartesian product
will be:
R x S
CARTESIAN PRODUCT
– Denoted by R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm)
 Number of attributes in resultant Relation
– The resulting relation will be containing all the attributes
of R and all of S.
 If R has n and S has m attribute then Result is a relation Q with degree
n + m attributes:
 Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order
 Number of tuple in resultant relation:
– If R has C tuples and S has D tuples, the result is
C*D tuples.
Lets R=2 tuple, S= 4 tuple
Tuples in RXS= 2*4=8
and R X S X R= 2*4*2=12 and so on
Cartesian Product (X) Example
Retrieve name of courses for which each student registered

Course x Registration

Course Registration
CID CourseTitle SID Name CourseId

C3456 Database Systems


S101 Ali Tahir C3456
C4567 Financial Management
S103 Farah Hasan C5678
C5678 Money & Capital Market
Cartesian Product (X) Example
StudentCourses=Course X Registration

CID CourseTitle SID Name CSID


C3456 Database Systems S101 Ali Tahir C3456
C4567 Financial Management S101 AliTahr C3456
C5678 Money & Capital Market S101 Ali Tahir C3456

C3456 Database Systems S103 Farah Hasan C5678

C4567 Financial Management S103 Farah Hasan C5678

C5678 Money & Capital Market S103 Farah Hasan C5678


 Generally, CROSS PRODUCT is not a
meaningful operation
– Can become meaningful when followed by
other operations such as Select

 E.g. in above example only two rows are valid if we want


to see which student register for which course then result
can filtered by using other operation such as:

Name, CourseTitle (CID=CSID StudentCourses)


CARTESIAN PRODUCT: Exampe

 Retrieve a list of names of each female


employee’s dependents

– FEMALE_EMPS   SEX=’F’(EMPLOYEE)
– EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)
– EMP_DEPENDENTS  EMPNAMES x DEPENDENT

 Result will not be meaningful


Query refer to this database state
FEMALE_EMPS   SEX=’F’(EMPLOYEE)

EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)

EMP_DEPENDENTS  EMPNAMES x DEPENDENT


EMP_DEPENDENTS  EMPNAMES x DEPENDENT

Rows = 3 * 7= 21
Out of 21 rows only one row is valid, which can be selected by using
select operator Slide 1-50
Example of applying CARTESIAN PRODUCT
 To keep only combinations where the
DEPENDENT is related to the EMPLOYEE,
we add a SELECT operation as follows

ACTUAL_DEPS   SSN=ESSN(EMP_DEPENDENTS)

RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)


Slide 1-52
More Binary Relational Operations

 Join

 Division
JOIN
 JOIN is a special form of cross product of two tables.
– In Cartesian product we join a tuple of one table with the every
tuples of the second table.
 But in join there is a special requirement of relationship
between tuples.
– For example if there is a relation STUDENT and a relation
BOOK then it may be required to know that how many books
have been issued to any particular student.
– Now in this case the primary key of STUDENT that is stId is a
foreign key in BOOK table through which the join can be
made.
 JOIN allows us to process relationships among relations.
JOIN
 JOIN Operation (denoted by )
– Join is a special form of cross product of two tables. In
Cartesian product we join a tuple of one table with the tuples
of the second table.
– The problem in CARTESIAN PRODUCT is that all
combinations of tuples are included in the result
 The sequence of CARTESIAN PRODECT followed by SELECT is
used quite commonly to identify and select related tuples from two
relations

– JOIN is binary operation that allows combining


certain selections and a Cartesian product into one
operation.
 It combines only those tuples which appear in both relational schema based
upon matching condition.
General form of JOIN
– The general form of a join operation on two relations
R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R <join condition> S

– Result is a relation Q with degree n + m attributes:


 Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.

– The resulting relation state has one tuple for each combination of
tuples—r from R and s from S, but only if they satisfy the join
condition r[Ai]=s[Bj]

– Hence, if R has nR tuples, and S has nS tuples, then the join result
will generally have less than nR * nS tuples.
JOIN: Example
 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.

DEPARTMENT MGRSSN=SSN EMPLOYEE

– The JOIN operation can be specified as a CARTESIAN PRODUCT


operation followed by a SELECT operation.
 MGRSSN=SSN (DEPARTMENT x EMPLOYEE) = DEPARTMENT MGRSSN=SSN EMPLOYEE

These two operation are use in single operation using JOIN


Query refer to this database state
Example of applying the JOIN
operation

Notice that in the result of an EQUIJOIN we always have one or


more pairs of attributes that have identical values in every tuple
JOIN types
 The general case of JOIN operation is called a
Theta-join: R theta S

 The join condition is called theta

 Theta can be any general boolean expression on


the attributes of R and S; for example:
– R.Ai<S.Bj AND (R.Ak=S.Bl OR R.Ap<S.Bq)
EQUIJOIN
 In Join condition in theta Join is equality operator
(=)that is called EQUIJOIN
 The JOIN seen in the previous example was an
EQUIJOIN

Common attributes appears twice in the output


NATURAL JOIN
 Another variation of JOIN called NATURAL JOIN —
denoted by *
– It was created to get rid of the repeated attribute values in
an EQUIJOIN condition.
– Common attributes appears once in the output

 DEPT_MGR  DEPARTMENT * EMPLOYEE

It will keep only one attribute i.e Mng_ssn


An implicit join condition is created based on this attribute:
DEPARTMENT.MgrSssn=Employee.Ssn
NATURAL JOIN: Example 1
 Another example: Q  R(A,B,C,D) * S(C,D,E)

R S Q
AB C D C D E A B C D E
3 6 2 3 4 1 9 3 6 2 3 7
4 8 4 1 2 3 7 4 8 4 1 9
– The implicit join condition includes each pair of attributes with the same
name, “AND”ed together:
 R.C=S.C AND R.D = S.D

– Result keeps only one attribute of each such pair:


 Q(A,B,C,D,E)
NATURAL JOIN: Example 2
 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

 Only attribute with the same name is DNUMBER

 An implicit join condition is created based on this attribute:

DEPARTMENT.DNUMBER=DEPT_LOCATIONS.DNUMBER
INNER JOIN
 The JOIN operations described earlier match
tuples that satisfy the join condition.
– For example, for a NATURAL JOIN operation R *
S, only tuples from R that have matching tuples in
S—and vice versa—appear in the result.
 Hence, tuples without a matching (or related)
tuple are eliminated from the JOIN result.
 Tuples with NULL values in the join attributes are
also eliminated.
 This type of join, where tuples with no match are
eliminated, is known as an Inner join.
– All the previous JOIN are Inner join
OUTER JOINS

 Other types of join are:


– Outer Join
– Left Outer Join
– Right Outer Join
– Semijoin
Left Outer Join
 Denoted by
– Keep all of the tuples from the “left” relation
– Join with the right relation
– Pad the non-matching tuples with nulls
Left Outer Join Example
FACULTY COURSE

facId facName dept salary rank crCode crTitle fId

F2345 Usman CSE 21000 lecturer C3456 Database Systems F2345

F3456 Tahir CSE 23000 Asso Prof C4567 Financial

F4567 Ayesha ENG 27000 Asso Prof Management

C5678 Money & Capital F4567


F5678 Samad MNG 32000 Professor
Market

C3425 Introduction to F2345


Accounting
Left Outer Join Example

FACULTY LEFT JOIN COURSE

facId facName dept salary rank crCode crTitle facId

F2345 Usman CSE 21000 lecturer C3456 Database Systems F2345

F2345 Usman CSE 21000 lecturer C3425 Intro. To Accounting F2345

F3456 Tahir CSE 23000 Asso Prof Null Null Null


Money & Capital Market
F4567 Ayesha ENG 27000 Asso Prof C5678 . To Accounting F4567
F5678 Samad MNG 32000 Professor Null Null Null
Right Outer Join Example
 Denoted by:
– Same as the left, but keep tuples from the
“right” relation
FACULTY RIGHT OUTER JOIN COURSE

facId facName dept salary rank crCode crTitle facId

F2345 Usman CSE 21000 lecturer C3456 Database Systems F2345

Null Null Null Null Null C4567 Financial Management Null


Money & Capital
F4567 Ayesha ENG 27000 Asso Prof C5678 Money & Capital Market
Market F4567
.

F2345 Usman CSE 21000 lecturer C3425


Intro. To Accounting
F2345
Outer JOIN
 Denoted by:
– Keep all tuples from both relations

FACULTY OUTER JOIN COURSE

facId facName dept salary rank crCode crTitle facId

F2345 Usman CSE 21000 lecturer C3456 Database Systems F2345

F2345 Usman CSE 21000 lecturer C3425 Intro. To Accounting F2345


Money & Capital
F4567 Ayesha ENG 27000 Asso Prof C5678 Money & Capital Market
Market F4567
.

F3456 Tahir CSE 23000 Asso Prof Null Null


Null

F5678 Samad MNG 32000 Professor Null Null


Null

Null Null Null Null Null C4567 Financial Management Null


SEMI- JOIN
 First take the natural join of two tables
 Then take the projection on the attributes of first table

FACULTY COURSE

facId facName dept salary rank

F2345 Usman CSE 21000 lecturer

F4567 Ayesha ENG 27000 Asso Prof

Notice only one row for facId=F2345, since project eliminate


duplicate
Complete Set of Relational
Operations
 The set of operations including
– SELECT , PROJECT  , UNION ,
DIFFERENCE  , RENAME , and CARTESIAN
PRODUCT X is called a complete set because any
other relational algebra expression can be
expressed by a combination of these five
operations.
 For example:
– R  S = (R  S ) – ((R  S)  (S  R))
– R <join condition>S =  <join condition> (R X S)
Examples of Queries in Relational
Algebra
 Q1: Retrieve the name and address of all employees
who work for the ‘Research’ department.
The following query results refer
to this database state
Examples of Queries in Relational
Algebra
 Q2: For every project located in “Stafford”, list the
project number, the controlling department number,
and the department manager’s last name, address, and
Bdate
The DIVISION Operation

 Denoted by ÷
– Example: retrieve the names of employees who
work on all the projects that ‘John Smith’
works on
 Apply to relations R(Z) ÷ S(X)
– Attributes of R are a subset of the attributes of S
Example of DIVISION

 An example is Retrieve the names of


employees who work on all the projects that
‘John Smith’ works on.
Example of DIVISION
Examples of Queries in Relational
Algebra
 Q3: Find the names of employees who work on ALL
projects controlled by department 5
Operations of Relational Algebra
Operations of Relational Algebra
(cont’d.)
Notation for Query Trees

 Query tree
– Represents the input relations of query as leaf
nodes of the tree
– Represents the relational algebra operations as
internal nodes
Q2: For every project located in “Stafford”, list the project
number, the controlling department number, and the
department manager’s last name, address, and Bdate
Relational Algebra Query Trees
List all the managers that work in the sales department.

SELECT * There are at least three alternative


FROM emp, dept ways of representing this query as a
WHERE emp.deptno = dept.deptno Relational Algebra expression.
AND emp.job = ‘Manager’ Draw Query Tree
AND dept.name = ‘Sales’;

(job = ‘Manager’)  (name=‘Sales’)  (emp.deptno = dept.deptno) (EMP X DEPT)

(job = ‘Manager’)  (name=‘Sales’) (EMP emp.deptno = dept.deptno DEPT)

((job = ‘Manager’) (EMP)) emp.deptno = dept.deptno ((name=‘Sales’) (DEPT))


Relational Algebra Query Trees
A Relational Algebra query can be represented as a ‘query tree’. For
example the query to list all the managers that work in the sales
department could be described as one of the following:

(job = ‘Manager’)  (name=‘Sales’)  (emp.deptno = dept.deptno) (EMP X DEPT)

(job = ‘Manager’)  (name=‘Sales’) Root

 (emp.deptno = dept.deptno)
Intermediate
operations
X

Leaves
EMP DEPT
Relational Algebra Query Trees
Alternative‘query tree’ for the query to list all the managers that work
in the sales department:

(job = ‘Manager’)  (name=‘Sales’) (EMP emp.deptno = dept.deptno DEPT)

(job = ‘Manager’)  (name=‘Sales’)

emp.deptno = dept.deptno

EMP DEPT
Relational Algebra Query Trees
Alternative‘query tree’ for the query to list all the managers that work
in the sales department:

((job = ‘Manager’) (EMP)) emp.deptno = dept.deptno ((name=‘Sales’) (DEPT))

emp.deptno = dept.deptno

(job = ‘Manager’) (name=‘Sales’)

EMP DEPT
Additional Relational Operations

 Some common database requests --- which are


needed in commercial applications for DBMS
--- cannot be performed with the original
relational algebra operations

 In this section, we define additional operations


to express these requests
Generalized Projection

 It extend the projection by allowing functions


of attributes to be included in the projection
list.

 The general form is:


F1, F2, …, Fn (R)

where F1, F2,…Fn are functions over the attributes


Generalized Projection
 For example Consider the relation
Employee (Ssn, salary, Deduction, Year_service)

A report may be required to show:


Net_salary = salary – Deduction
Bonus = 2000 * year_service
Tax = Salary * 25%

Then a generalized projection combined with renaming may be:

Report  (Ssn, Net_salary, Bonus, Tax )


( Ssn, salary-deduction, 2000*years_service, salary*25% (R))
Aggregate Functions and Grouping

 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.
Aggregate Functions and Grouping

 Common functions applied to collections of


numeric values include
– SUM, AVERAGE, MAXIMUM, and
MINIMUM.
 The COUNT function is used for counting
tuples or values.
Aggregate Functions and Grouping
 Defined using the symbol ℑ (pronounced script F), to
specify these types of requests as follows:

– where <grouping attributes> is a list of attributes of the


relation specified in R
– <function list> is a list of (<function> <attribute>) pairs.
– In each such pair, <function> is one of the allowed
functions—such as:
 SUM, AVERAGE, MAXIMUM, MINIMUM,COUNT—and
<attribute> is an attribute of the relation specified by R.
– The resulting relation has the grouping attributes plus one
attribute for each element in the function list.
Aggregate Functions and Grouping
 Use of the Aggregate Functional operation Ʒ
– Ʒ 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

– Ʒ COUNT SSN, AVERAGE Salary (EMPLOYEE) computes the count


(number) of employees and their average salary
Aggregate Functions and Grouping
 Grouping can be combined with Aggregate Functions

 Example: For each department, retrieve the DNO, COUNT


SSN, and AVERAGE SALARY

 A variation of aggregate operation Ʒ allows this:


– Grouping attribute placed to left of symbol
– Aggregate functions to right of symbol
– DNO Ʒ COUNT SSN, AVERAGE Salary (EMPLOYEE)
Illustrating aggregate functions
and grouping
Examples of applying aggregate
functions and grouping
Examples of Queries in Relational
Algebra
 Q5: List the names of all employees with two or more
dependents
Examples of Queries in Relational
Algebra
 Q6: Retrieve the names of employees who have no
dependents.
Examples of Queries in Relational
Algebra
 Q7: List the names of managers who have at least one
dependent
Recursive Closure Operations

 Operation applied to a recursive relationship


between tuples of same type
Exercise
Examples of Queries
in Relational Algebra (cont’d.)
Examples of Queries
in Relational Algebra (cont’d.)
Summary

 Formal languages for relational model of data:


– Relational algebra: operations, unary and
binary operators
– Some queries cannot be stated with basic
relational algebra operations
But are important for practical use

You might also like