SlideShare a Scribd company logo
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
 There are two types of operations in RDBMS
 Retrieval
 Update
 The set of operations for specifying retrieval
requests (or queries) in relational model is called
Relational Algebra.
 A sequence of relational algebra operations forms a
relational algebra expression.
COMPANY DATABASE
CONSIDERED IN EXAMPLES
SELECT OPERATION(UNARY OPERATION)
 This operation selects a subset of tuples from a relation
that satisfy a selection condition.
 Select is denoted by :  <selection condition>(R)
EXAMPLES : SELECT OPERATION
 Select the employees whose department number is 4:
 DNO = 4 (EMPLOYEE)
 Select all the projects in department 5
 Select the employees whose salary is greater than
$35,000
Slide
5-
6
5.6
SELECT OPERATION
 Selection condition is a Boolean expression specified on the
attributes of relation R
 It can include boolean operators AND, OR, NOT applied on relational
operators <, > <=,>=, !=, =
 Select  is commutative:
 <condition1>( < condition2> (R)) =  <condition2> ( < condition1> (R))
 Cascade of Select operations
<cond1>(< cond2> (<cond3>(R)) =  <cond1> AND < cond2> AND < cond3>(R)))
PROJECT OPERATION (UNARY OPERATION)
 This operation selects a subset of columns from the existing
relation.
 Project operation is denoted by <attribute list>R
 It removes duplicate tuples, the result of project is set of tuples
 Example:
 RESULT  LNAME, FNAME, SALARY (EMPLOYEE)
 DN  DNAME, DNUMBER (DEPARTMENT)
PROJECT OPERATION
 Project operation is not commutative
  <list1> ( <list2> (R) ) =  <list1> (R) as long as <list2> contains the
attributes in <list1>
 No of Tuples in the result of projection π <list>(R)
 less or equal to the number of tuples in R
 If the list of attributes includes a key of R, then the no of is equal
to the no of tuples in R
RELATIONAL ALGEBRA EXPRESSIONS
 We may want to apply several relational algebra
operations one after the other
1. We can write the operations as a single
relational algebra expression by nesting the
operations, or
2. We can apply one operation at a time and
create intermediate result relations.
EXAMPLE: SEQUENCE OF OPERATIONS
 To retrieve the first name, last name, and salary of all
employees who work in Department 5
 Result of sequence of operations:
 FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))
 Using intermediate relation:
 D5   DNO=5(EMPLOYEE)
 RESULT   FNAME, LNAME, SALARY (D5)
 Renaming of attributes
 D5   DNO=5(EMPLOYEE)
 R (FirstName,LastName,Salary)   FNAME, LNAME, SALARY (D5)
EXAMPLE OF APPLYING MULTIPLE OPERATIONS
AND RENAME
FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))
D5   DNO=5(EMPLOYEE)
R (First_name,Last_name,Salary)   Fname, Lname, Salary (D5)
RENAME OPEARATION
 Rename operator is denoted by  (rho)
 Rename operation  can be expressed as:
 S(R) rename the relation to S
 (B1, B2, …, Bn )(R) rename the attributes to B1, B2, …..Bn
 S (B1, B2, …, Bn )(R) rename both relation to S, and attributes to
B1, B1, …..Bn
 Example:
  RESULT (First_Name,Last_Name, DNO)(D5)
UNION (BINARY OPERATION)
 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
 The two relations R and S must be “type compatible” (or
Union compatible)
 R and S must have same number of attributes
 Each pair of corresponding attributes must have same or
compatible domains
UNION 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
DEP5_EMPS  DNO=5 (EMPLOYEE)
RESULT1   SSN(DEP5_EMPS)
RESULT2(SSN)  SUPERSSN(DEP5_EMPS)
RESULT  RESULT1  RESULT2
16
INTERSECTION AND SET DIFFERENCE (BINARY
OPERATIONS)
 INTERSECTION operation: the result of R  S, is a
relation that includes all tuples that are in both R and S
 SET DIFFERENCE operation: the result of R – S, is a
relation that includes all tuples that are in R but not in S
 Two relations R and S must be “type compatible”
RELATIONAL ALGEBRA OPERATIONS FROM SET
THEORY
 Both  and  are commutative operations
 R  S = S  R, and R  S = S  R
 Both  and  can be treated as n-ary operations
 R  (S  T) = (R  S)  T
 (R  S)  T = R  (S  T)
 Minus operation is not commutative
 R – S ≠ S – R
EXAMPLE TO ILLUSTRATE THE RESULT OF
UNION, INTERSECT, AND DIFFERENCE
CARTESIAN PRODUCT
 The result of Cartesian product of two relations
R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm) is given as:
Result(A1, A2, . . ., An, B1, B2, . . ., Bm)
 Let |R| = nR and |S| = nS , then |R x S|= nR * nS
 R and S may NOT be "type compatible”
 Cross Product is a meaningful operation only if it is
followed by other operations
Example (not meaningful):
F   SEX=’F’(EMPLOYEE)
EN   FNAME, LNAME, SSN (F)
E_DP  EN x DEPENDENT
Example (meaningful):
A_DP   SSN=ESSN(E_DP)
R   FNAME, LNAME, DEPENDENT_NAME(A_DP)
Problem:
Retrieve a list of each female
employee’s dependents
JOIN(BINARY OPERATION)
 JOIN denoted by combine related tuples from various
relations
 JOIN combines CARTESIAN PRODECT and SELECT
into a single operation
 General form of a join operation on two relations R(A1,
A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R <join condition>S
21
EXAMPLE OF JOIN OPERATION
 Retrieve the name of the manager of each department.
DEPT_MGR  DEPARTMENT MGRSSN=SSN EMPLOYEE
 The join condition can also be specified as
DEPARTMENT.MGRSSN= EMPLOYEE.SSN
22
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 relational
algebra expression can be expressed using these.
 For example:
 R  S = (R  S ) – ((R - S)  (S - R))
 R <join condition>S =  <join condition> (R X S) 23
SOME PROPERTIES OF JOIN
 Consider the following JOIN operation:
 R(A1, A2, . . ., An) S(B1, B2, . . ., Bm)
R.Ai=S.Bj
 Result is a relation Q with degree n + m attributes:
 Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
 Relation Q 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]
 If R has nR tuples, and S has nS tuples, then no of
tuples in join result < nR * nS . 24
THETA-JOIN
 The general case of JOIN operation is called a
Theta-join: R S
theta
 Theta is a 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)
 Theta can have any comparison operators
{=,≠,<,≤,>,≥,}
25
EQUI-JOIN
 EQUIJOIN is a join condition that involves only equality
operator = .
 Example:
 DEPT_MGR  DEPARTMENT MGRSSN=SSN EMPLOYEE
 Retrieve a list of each female employee’s dependents
F   SEX=’F’(EMPLOYEE)
EN   FNAME, LNAME, SSN (F)
E_DP  EN DEPENDENT
SSN=ESSN
26
ISSUE WITH EQUIJOIN OPERATION
 Superfluous column
 Result of EQUIJOIN always have one or more pairs of
attributes that have identical values in every tuple.
NATURAL JOIN OPERATION
 NATURAL JOIN operation (denoted by *) is created to
get rid of the superfluous attribute in an EQUIJOIN
condition.
 The two join attributes, or each pair of corresponding join
attributes must have the same name in both relations
 If this is not the case, a renaming operation is applied first.
28
NATURAL JOIN OPERATION
 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
29
EXAMPLE: NATURAL JOIN
 Another example: Q  R(A,B,C,D) * S(C,D,E)
 The implicit join condition includes each pair of attributes with the
same name, “AND” 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)
30
EXAMPLE OF NATURAL JOIN OPERATION
31
DIVISION (BINARY OPERATION)
 The division operation is applied to two relations
R(Z)  S(X), where X  Z.
 Let Y = Z – X
 We have Z = X  Y and Y is a set of attributes of R that are
not the attributes of S.
32
 The result of DIVISION is a relation T(Y)
 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.
EXAMPLE OF DIVISION
33
 Smith   fname=‘John’ and lname=‘Smith’ (Employee)
 Smith_Pnos   Pno (Works_on essn=ssn Smith)
 Ssn_Pnos   Essn,Pno (Works_on)
 SSNS(ssn) Ssn_Pnos  Smith_Pnos
Retrieve all employees who work on all the project
that John Smith works on
RECAP OF RELATIONAL ALGEBRA
OPERATIONS
34
AGGREGATE FUNCTIONS
 Now we specify mathematical aggregate functions on
collections of values from the database.
 Examples:
 Retrieve the average or total salary of all employees
 Retrieve total number of employee tuples
 Functions applied to collections of numeric values
include
 SUM, AVERAGE, MAXIMUM, and MINIMUM.
 COUNT function is used for counting tuples or values.
35
AGGREGATE FUNCTION OPERATION
 Use of the Aggregate Functional operation ℱ
 ℱMAX Salary (EMPLOYEE)
 ℱMIN Salary (EMPLOYEE)
 ℱSUM Salary (EMPLOYEE)
 ℱCOUNT SSN, AVERAGE Salary (EMPLOYEE)
 computes no of employees and their average salary
 Note: count just counts the number of rows, without
removing duplicates
36
USING GROUPING WITH AGGREGATION
 Grouping can be combined with Aggregate Functions
 Example:
 For each department, retrieve the DNO, COUNT SSN, and
AVERAGE SALARY
 DNO ℱCOUNT SSN, AVERAGE Salary (EMPLOYEE)
37
EXAMPLE: AGGREGATE FUNCTIONS AND GROUPING
38
EXAMPLES OF QUERIES IN RELATIONAL
ALGEBRA
39
 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= DNO EMPLOYEE)
RESULT   FNAME, LNAME, ADDRESS (RESEARCH_EMPS)
EXAMPLES OF QUERIES IN RELATIONAL
ALGEBRA
40
 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)
EXAMPLES OF QUERIES IN RELATIONAL
ALGEBRA
41
 Q5: Retrieve the names of all employees with two or more dependents.
T1(Ssn, No_of_dependents)  Essn ℱ COUNT Dependent_name (DEPENDENT)
T2   No_of_dependents >1(T1)
RESULT   LNAME, FNAME (T2 * EMPLOYEE)
ASSIGNMENT 2
 Section B: Due date 13-Feb 2013
 Section C: Due date 16-Feb 2013
OUTER JOIN OPERATION
 In INNER JOIN, tuples without a matching are
eliminated from the join result
 Tuples with null are also eliminated
 This amounts to loss of information.
 OUTER joins operations are used when we want
to keep
 all the tuples in R in the join result , or
 all tuples in S in the join result, or
 all tuples in both relations R and S in the join result
43
LEFT OUTER JOIN
 List the employees name and the department name that they
manage. If they don’t manage one, then indicate this with a
null value.
 Temp  (Employee Ssn=Mgr_Ssn Department)
 Result   Fname, Minit, Lname, Dname(Temp)
44
OUTER JOIN OPERATION
 Left outer join: keeps every tuple in R, denoted as
R S
 if no matching tuple is found in S, then the attributes of
S in the join result are filled with null values.
 Right outer join: keeps every tuple in S in the
result of R S.
 Full outer join: keeps all tuples in both the left and
the right relations. It is denoted by
45
FULL OUTER JOIN VS CARTESIAN PRODUCT
Employee Ssn=Mgr_Ssn Department
??
OUTER UNION OPERATIONS
 The outer union operation take the union of tuples
in two relations R(X, Y) and S(X, Z) that are
partially compatible,
 Only some of their attributes, say X, are type
compatible.
 The attributes that are type compatible are represented
only once in the result
 The attributes that are not type compatible from either
relation are also kept in the result relation T(X, Y, Z).
47
OUTER JOIN EXAMPLE
 An outer union can be applied to two relations STUDENT(Name,
SSN, Department, Advisor) and INSTRUCTOR(Name, SSN,
Department, Rank).
 Tuples 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.
 Result relation:
STUDENT_OR_INSTRUCTOR (Name, SSN, Department,
Advisor, Rank)
48
RECURSIVE CLOSURE OPERATION
 This can’t be specified in general using Relational Algebra
 Example: Retrieve all SUPERVISEES of an EMPLOYEE e at all
levels — that is,
 all employees 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.
 We can retrieve employees at each level and then take their union,
however, we cannot 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.
49
RECURSIVE CLOSURE OPERATION
50
51
Example of Query Tree
Query: For every project located in ‘Stafford’, list the project number, the
controlling department number, and the department manager’s last name,
address, and birth date.
QUERY TREE
 An internal data structure to represent a query
 Standard technique to estimate the work done in executing
the query, and the optimization of execution
 Nodes stand for operations like selection, projection, join,
renaming, division, ….
 Leaf nodes represent base relations
 A tree gives a good visual feel of the complexity of the query
and the operations involved
 Algebraic Query Optimization consists of rewriting the query
or modifying the query tree into an equivalent tree.
52
RELATIONAL ALGEBRA OPERATORS
 Relational Algebra consists of several groups of operations
 Unary Relational Operations
 SELECT (symbol:  (sigma))
 PROJECT (symbol:  (pi))
 RENAME (symbol:  (rho))
 Relational Algebra Operations From Set Theory
 UNION (  ), INTERSECTION (  ), DIFFERENCE (–)
 CARTESIAN PRODUCT ( x )
 Binary Relational Operations
 JOIN (several variations of JOIN exist)
 DIVISION
 Additional Relational Operations
 OUTER JOINS, OUTER UNION
 AGGREGATE FUNCTIONS (These compute summary of
information: for example, SUM, COUNT, AVG, MIN, MAX)
CHAPTER SUMMARY
 Relational Algebra
 Unary Relational Operations
 Relational Algebra Operations From Set Theory
 Binary Relational Operations
 Additional Relational Operations
 Examples of Queries in Relational Algebra
54

More Related Content

PPT
Module 2-2.ppt
PDF
DBMS Module 2.2.pdf......................
PPT
Relational-algebra in Data base management ppts
PPTX
Query and optimizing operating system.pptx
PPTX
Relational Algebra.pptx for Module four
PPT
DOCX
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
PPT
E212d9a797dbms chapter3 b.sc2
Module 2-2.ppt
DBMS Module 2.2.pdf......................
Relational-algebra in Data base management ppts
Query and optimizing operating system.pptx
Relational Algebra.pptx for Module four
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
E212d9a797dbms chapter3 b.sc2

Similar to 3._Relational_Algebra.pptx:Basics of relation algebra (20)

PPT
E212d9a797dbms chapter3 b.sc2 (1)
PPT
E212d9a797dbms chapter3 b.sc2 (2)
PPT
Chapter6
PPT
Dbms ii mca-ch5-ch6-relational algebra-2013
PPTX
Relational algebr
PPT
Mapping of Multivalued attributesER-to-Relational Mapping Algorithm.ppt
PPTX
relalgebra-220717082803-22f6cf31_2 - Copy.pptx
PPTX
lecture 4 Relational Algebra my sql work
PPT
Relational algebra-and-relational-calculus
PPT
relational algebra IN DATABASE MANAGEMENT SYSTEM COURSE FOR 4TH SEM VTU
PDF
1695304562_RELATIONAL_ALGEBRA.pdf
PDF
5 the relational algebra and calculus
PPT
Ra Revision
PDF
Cs501 rel algebra
PDF
Unit-II DBMS presentation for students.pdf
PPTX
Module 2_Relational Algebra.pptx
PPT
Relational algebra operations
PPT
relalgebraasssssssssssssssssssssssss.ppt
PDF
Chapter – 5 Relational Algebra.pdf
PPT
Database systemhe Relational Alge. goodbra.
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (2)
Chapter6
Dbms ii mca-ch5-ch6-relational algebra-2013
Relational algebr
Mapping of Multivalued attributesER-to-Relational Mapping Algorithm.ppt
relalgebra-220717082803-22f6cf31_2 - Copy.pptx
lecture 4 Relational Algebra my sql work
Relational algebra-and-relational-calculus
relational algebra IN DATABASE MANAGEMENT SYSTEM COURSE FOR 4TH SEM VTU
1695304562_RELATIONAL_ALGEBRA.pdf
5 the relational algebra and calculus
Ra Revision
Cs501 rel algebra
Unit-II DBMS presentation for students.pdf
Module 2_Relational Algebra.pptx
Relational algebra operations
relalgebraasssssssssssssssssssssssss.ppt
Chapter – 5 Relational Algebra.pdf
Database systemhe Relational Alge. goodbra.
Ad

More from ZakriyaMalik2 (8)

PPT
Automated Testing v s Manual Testing.ppt
PPTX
Group - 9 _ Github Actions.pptx
PPTX
Software Configuration Management.pptx
PPTX
Object Oriented Testing Strategy.pptx
PPTX
Project PPT.pptx
PPT
Automated Testing vs Manual Testing.ppt
PDF
Operating System Lecture 4 on Different Topics
PDF
Database :Introduction to Database System
Automated Testing v s Manual Testing.ppt
Group - 9 _ Github Actions.pptx
Software Configuration Management.pptx
Object Oriented Testing Strategy.pptx
Project PPT.pptx
Automated Testing vs Manual Testing.ppt
Operating System Lecture 4 on Different Topics
Database :Introduction to Database System
Ad

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
01-Introduction-to-Information-Management.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Institutional Correction lecture only . . .
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Insiders guide to clinical Medicine.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Cell Types and Its function , kingdom of life
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Pre independence Education in Inndia.pdf
O7-L3 Supply Chain Operations - ICLT Program
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Cell Structure & Organelles in detailed.
O5-L3 Freight Transport Ops (International) V1.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
01-Introduction-to-Information-Management.pdf
Microbial disease of the cardiovascular and lymphatic systems
Computing-Curriculum for Schools in Ghana
Microbial diseases, their pathogenesis and prophylaxis
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Institutional Correction lecture only . . .
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Insiders guide to clinical Medicine.pdf
Anesthesia in Laparoscopic Surgery in India
GDM (1) (1).pptx small presentation for students
Cell Types and Its function , kingdom of life
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Pre independence Education in Inndia.pdf

3._Relational_Algebra.pptx:Basics of relation algebra

  • 2. RELATIONAL ALGEBRA  There are two types of operations in RDBMS  Retrieval  Update  The set of operations for specifying retrieval requests (or queries) in relational model is called Relational Algebra.  A sequence of relational algebra operations forms a relational algebra expression.
  • 4. SELECT OPERATION(UNARY OPERATION)  This operation selects a subset of tuples from a relation that satisfy a selection condition.  Select is denoted by :  <selection condition>(R)
  • 5. EXAMPLES : SELECT OPERATION  Select the employees whose department number is 4:  DNO = 4 (EMPLOYEE)  Select all the projects in department 5  Select the employees whose salary is greater than $35,000
  • 7. SELECT OPERATION  Selection condition is a Boolean expression specified on the attributes of relation R  It can include boolean operators AND, OR, NOT applied on relational operators <, > <=,>=, !=, =  Select  is commutative:  <condition1>( < condition2> (R)) =  <condition2> ( < condition1> (R))  Cascade of Select operations <cond1>(< cond2> (<cond3>(R)) =  <cond1> AND < cond2> AND < cond3>(R)))
  • 8. PROJECT OPERATION (UNARY OPERATION)  This operation selects a subset of columns from the existing relation.  Project operation is denoted by <attribute list>R  It removes duplicate tuples, the result of project is set of tuples  Example:  RESULT  LNAME, FNAME, SALARY (EMPLOYEE)  DN  DNAME, DNUMBER (DEPARTMENT)
  • 9. PROJECT OPERATION  Project operation is not commutative   <list1> ( <list2> (R) ) =  <list1> (R) as long as <list2> contains the attributes in <list1>  No of Tuples in the result of projection π <list>(R)  less or equal to the number of tuples in R  If the list of attributes includes a key of R, then the no of is equal to the no of tuples in R
  • 10. RELATIONAL ALGEBRA EXPRESSIONS  We may want to apply several relational algebra operations one after the other 1. We can write the operations as a single relational algebra expression by nesting the operations, or 2. We can apply one operation at a time and create intermediate result relations.
  • 11. EXAMPLE: SEQUENCE OF OPERATIONS  To retrieve the first name, last name, and salary of all employees who work in Department 5  Result of sequence of operations:  FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))  Using intermediate relation:  D5   DNO=5(EMPLOYEE)  RESULT   FNAME, LNAME, SALARY (D5)  Renaming of attributes  D5   DNO=5(EMPLOYEE)  R (FirstName,LastName,Salary)   FNAME, LNAME, SALARY (D5)
  • 12. EXAMPLE OF APPLYING MULTIPLE OPERATIONS AND RENAME FNAME, LNAME, SALARY( DNO=5(EMPLOYEE)) D5   DNO=5(EMPLOYEE) R (First_name,Last_name,Salary)   Fname, Lname, Salary (D5)
  • 13. RENAME OPEARATION  Rename operator is denoted by  (rho)  Rename operation  can be expressed as:  S(R) rename the relation to S  (B1, B2, …, Bn )(R) rename the attributes to B1, B2, …..Bn  S (B1, B2, …, Bn )(R) rename both relation to S, and attributes to B1, B1, …..Bn  Example:   RESULT (First_Name,Last_Name, DNO)(D5)
  • 14. UNION (BINARY OPERATION)  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  The two relations R and S must be “type compatible” (or Union compatible)  R and S must have same number of attributes  Each pair of corresponding attributes must have same or compatible domains
  • 15. UNION 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 DEP5_EMPS  DNO=5 (EMPLOYEE) RESULT1   SSN(DEP5_EMPS) RESULT2(SSN)  SUPERSSN(DEP5_EMPS) RESULT  RESULT1  RESULT2
  • 16. 16 INTERSECTION AND SET DIFFERENCE (BINARY OPERATIONS)  INTERSECTION operation: the result of R  S, is a relation that includes all tuples that are in both R and S  SET DIFFERENCE operation: the result of R – S, is a relation that includes all tuples that are in R but not in S  Two relations R and S must be “type compatible”
  • 17. RELATIONAL ALGEBRA OPERATIONS FROM SET THEORY  Both  and  are commutative operations  R  S = S  R, and R  S = S  R  Both  and  can be treated as n-ary operations  R  (S  T) = (R  S)  T  (R  S)  T = R  (S  T)  Minus operation is not commutative  R – S ≠ S – R
  • 18. EXAMPLE TO ILLUSTRATE THE RESULT OF UNION, INTERSECT, AND DIFFERENCE
  • 19. CARTESIAN PRODUCT  The result of Cartesian product of two relations R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm) is given as: Result(A1, A2, . . ., An, B1, B2, . . ., Bm)  Let |R| = nR and |S| = nS , then |R x S|= nR * nS  R and S may NOT be "type compatible”  Cross Product is a meaningful operation only if it is followed by other operations
  • 20. Example (not meaningful): F   SEX=’F’(EMPLOYEE) EN   FNAME, LNAME, SSN (F) E_DP  EN x DEPENDENT Example (meaningful): A_DP   SSN=ESSN(E_DP) R   FNAME, LNAME, DEPENDENT_NAME(A_DP) Problem: Retrieve a list of each female employee’s dependents
  • 21. JOIN(BINARY OPERATION)  JOIN denoted by combine related tuples from various relations  JOIN combines CARTESIAN PRODECT and SELECT into a single operation  General form of a join operation on two relations R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is: R <join condition>S 21
  • 22. EXAMPLE OF JOIN OPERATION  Retrieve the name of the manager of each department. DEPT_MGR  DEPARTMENT MGRSSN=SSN EMPLOYEE  The join condition can also be specified as DEPARTMENT.MGRSSN= EMPLOYEE.SSN 22
  • 23. 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 relational algebra expression can be expressed using these.  For example:  R  S = (R  S ) – ((R - S)  (S - R))  R <join condition>S =  <join condition> (R X S) 23
  • 24. SOME PROPERTIES OF JOIN  Consider the following JOIN operation:  R(A1, A2, . . ., An) S(B1, B2, . . ., Bm) R.Ai=S.Bj  Result is a relation Q with degree n + m attributes:  Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.  Relation Q 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]  If R has nR tuples, and S has nS tuples, then no of tuples in join result < nR * nS . 24
  • 25. THETA-JOIN  The general case of JOIN operation is called a Theta-join: R S theta  Theta is a 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)  Theta can have any comparison operators {=,≠,<,≤,>,≥,} 25
  • 26. EQUI-JOIN  EQUIJOIN is a join condition that involves only equality operator = .  Example:  DEPT_MGR  DEPARTMENT MGRSSN=SSN EMPLOYEE  Retrieve a list of each female employee’s dependents F   SEX=’F’(EMPLOYEE) EN   FNAME, LNAME, SSN (F) E_DP  EN DEPENDENT SSN=ESSN 26
  • 27. ISSUE WITH EQUIJOIN OPERATION  Superfluous column  Result of EQUIJOIN always have one or more pairs of attributes that have identical values in every tuple.
  • 28. NATURAL JOIN OPERATION  NATURAL JOIN operation (denoted by *) is created to get rid of the superfluous attribute in an EQUIJOIN condition.  The two join attributes, or each pair of corresponding join attributes must have the same name in both relations  If this is not the case, a renaming operation is applied first. 28
  • 29. NATURAL JOIN OPERATION  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 29
  • 30. EXAMPLE: NATURAL JOIN  Another example: Q  R(A,B,C,D) * S(C,D,E)  The implicit join condition includes each pair of attributes with the same name, “AND” 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) 30
  • 31. EXAMPLE OF NATURAL JOIN OPERATION 31
  • 32. DIVISION (BINARY OPERATION)  The division operation is applied to two relations R(Z)  S(X), where X  Z.  Let Y = Z – X  We have Z = X  Y and Y is a set of attributes of R that are not the attributes of S. 32  The result of DIVISION is a relation T(Y)  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.
  • 33. EXAMPLE OF DIVISION 33  Smith   fname=‘John’ and lname=‘Smith’ (Employee)  Smith_Pnos   Pno (Works_on essn=ssn Smith)  Ssn_Pnos   Essn,Pno (Works_on)  SSNS(ssn) Ssn_Pnos  Smith_Pnos Retrieve all employees who work on all the project that John Smith works on
  • 34. RECAP OF RELATIONAL ALGEBRA OPERATIONS 34
  • 35. AGGREGATE FUNCTIONS  Now we specify mathematical aggregate functions on collections of values from the database.  Examples:  Retrieve the average or total salary of all employees  Retrieve total number of employee tuples  Functions applied to collections of numeric values include  SUM, AVERAGE, MAXIMUM, and MINIMUM.  COUNT function is used for counting tuples or values. 35
  • 36. AGGREGATE FUNCTION OPERATION  Use of the Aggregate Functional operation ℱ  ℱMAX Salary (EMPLOYEE)  ℱMIN Salary (EMPLOYEE)  ℱSUM Salary (EMPLOYEE)  ℱCOUNT SSN, AVERAGE Salary (EMPLOYEE)  computes no of employees and their average salary  Note: count just counts the number of rows, without removing duplicates 36
  • 37. USING GROUPING WITH AGGREGATION  Grouping can be combined with Aggregate Functions  Example:  For each department, retrieve the DNO, COUNT SSN, and AVERAGE SALARY  DNO ℱCOUNT SSN, AVERAGE Salary (EMPLOYEE) 37
  • 38. EXAMPLE: AGGREGATE FUNCTIONS AND GROUPING 38
  • 39. EXAMPLES OF QUERIES IN RELATIONAL ALGEBRA 39  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= DNO EMPLOYEE) RESULT   FNAME, LNAME, ADDRESS (RESEARCH_EMPS)
  • 40. EXAMPLES OF QUERIES IN RELATIONAL ALGEBRA 40  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)
  • 41. EXAMPLES OF QUERIES IN RELATIONAL ALGEBRA 41  Q5: Retrieve the names of all employees with two or more dependents. T1(Ssn, No_of_dependents)  Essn ℱ COUNT Dependent_name (DEPENDENT) T2   No_of_dependents >1(T1) RESULT   LNAME, FNAME (T2 * EMPLOYEE)
  • 42. ASSIGNMENT 2  Section B: Due date 13-Feb 2013  Section C: Due date 16-Feb 2013
  • 43. OUTER JOIN OPERATION  In INNER JOIN, tuples without a matching are eliminated from the join result  Tuples with null are also eliminated  This amounts to loss of information.  OUTER joins operations are used when we want to keep  all the tuples in R in the join result , or  all tuples in S in the join result, or  all tuples in both relations R and S in the join result 43
  • 44. LEFT OUTER JOIN  List the employees name and the department name that they manage. If they don’t manage one, then indicate this with a null value.  Temp  (Employee Ssn=Mgr_Ssn Department)  Result   Fname, Minit, Lname, Dname(Temp) 44
  • 45. OUTER JOIN OPERATION  Left outer join: keeps every tuple in R, denoted as R S  if no matching tuple is found in S, then the attributes of S in the join result are filled with null values.  Right outer join: keeps every tuple in S in the result of R S.  Full outer join: keeps all tuples in both the left and the right relations. It is denoted by 45
  • 46. FULL OUTER JOIN VS CARTESIAN PRODUCT Employee Ssn=Mgr_Ssn Department ??
  • 47. OUTER UNION OPERATIONS  The outer union operation take the union of tuples in two relations R(X, Y) and S(X, Z) that are partially compatible,  Only some of their attributes, say X, are type compatible.  The attributes that are type compatible are represented only once in the result  The attributes that are not type compatible from either relation are also kept in the result relation T(X, Y, Z). 47
  • 48. OUTER JOIN EXAMPLE  An outer union can be applied to two relations STUDENT(Name, SSN, Department, Advisor) and INSTRUCTOR(Name, SSN, Department, Rank).  Tuples 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.  Result relation: STUDENT_OR_INSTRUCTOR (Name, SSN, Department, Advisor, Rank) 48
  • 49. RECURSIVE CLOSURE OPERATION  This can’t be specified in general using Relational Algebra  Example: Retrieve all SUPERVISEES of an EMPLOYEE e at all levels — that is,  all employees 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.  We can retrieve employees at each level and then take their union, however, we cannot 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. 49
  • 51. 51 Example of Query Tree Query: For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birth date.
  • 52. QUERY TREE  An internal data structure to represent a query  Standard technique to estimate the work done in executing the query, and the optimization of execution  Nodes stand for operations like selection, projection, join, renaming, division, ….  Leaf nodes represent base relations  A tree gives a good visual feel of the complexity of the query and the operations involved  Algebraic Query Optimization consists of rewriting the query or modifying the query tree into an equivalent tree. 52
  • 53. RELATIONAL ALGEBRA OPERATORS  Relational Algebra consists of several groups of operations  Unary Relational Operations  SELECT (symbol:  (sigma))  PROJECT (symbol:  (pi))  RENAME (symbol:  (rho))  Relational Algebra Operations From Set Theory  UNION (  ), INTERSECTION (  ), DIFFERENCE (–)  CARTESIAN PRODUCT ( x )  Binary Relational Operations  JOIN (several variations of JOIN exist)  DIVISION  Additional Relational Operations  OUTER JOINS, OUTER UNION  AGGREGATE FUNCTIONS (These compute summary of information: for example, SUM, COUNT, AVG, MIN, MAX)
  • 54. CHAPTER SUMMARY  Relational Algebra  Unary Relational Operations  Relational Algebra Operations From Set Theory  Binary Relational Operations  Additional Relational Operations  Examples of Queries in Relational Algebra 54