Outline - Emory University

26
Outline Relational Algebra Unary Relational Operations Select, Project Relational Algebra Operations From Set Theory Union, Intersection, Difference, Cartesian Product Binary Relational Operations Join, Set Division 35 Join, Set Division Additional Relational Operations Generalized project, Aggregates, Outer Join Examples and exercise Relational Calculus Tuple Relational Calculus Coming up SQL

Transcript of Outline - Emory University

Page 1: Outline - Emory University

Outline

� Relational Algebra

� Unary Relational Operations� Select, Project

� Relational Algebra Operations From Set Theory� Union, Intersection, Difference, Cartesian Product

� Binary Relational Operations� Join, Set Division

35

� Join, Set Division

� Additional Relational Operations� Generalized project, Aggregates, Outer Join

� Examples and exercise

� Relational Calculus

� Tuple Relational Calculus

� Coming up� SQL

Page 2: Outline - Emory University

Additional Relational Operations –

Generalized projection

� Allows functions of attributes to be included in the

projection list

36

� Example

πLNAME, FNAME, SALARY*1.03 (EMPLOYEE)

Page 3: Outline - Emory University

Additional Operations – Aggregate Functions

� An aggregate functions operates on a collection of

values (tuples) from the database and computes one

single value as output.

� Common functions include SUM, AVERAGE,

MAXIMUM, and MINIMUM and COUNT.

37

MAXIMUM, and MINIMUM and COUNT.

Page 4: Outline - Emory University

Aggregate Functions and Grouping

� Groups the tuples by the values of some attributes and then

apply an aggregate function independently to each group

38

Page 5: Outline - Emory University

Aggregation and Grouping – Formal

Notation

39

Page 6: Outline - Emory University

40

Page 7: Outline - Emory University

41

Page 8: Outline - Emory University

Operations of Relational Algebra

42

Page 9: Outline - Emory University

Operations of Relational Algebra

43

Page 10: Outline - Emory University

Complete Set of Relational Operations

� The set of operations including select σσσσ, project ππππ , union ∪∪∪∪, set difference - , and cartesian product Xis called a complete set because any other relational algebra expression can be expressed by a combination of these five operations.

44

� For example:

R ∩∩∩∩ S = (R ∪∪∪∪ S ) – ((R −−−− S) ∪∪∪∪ (S −−−− R))

R <join condition>S = σσσσ <join condition> (R X S)

Page 11: Outline - Emory University

Additional Operations

� Generalized Projections

� Aggregate and Groupings

� Outer Joins

45

Page 12: Outline - Emory University

Examples of Queries

in Relational Algebra

46

Page 13: Outline - Emory University

Examples of Queries

in Relational Algebra

47

Page 14: Outline - Emory University

1. Find fname and lname of all employees who have 2 or more

dependents

48

Page 15: Outline - Emory University

1. Find fname and lname of all employees who have 2 or more

dependents

49

Page 16: Outline - Emory University

2. Find fname and lname of the employees who have the most

number of dependents

50

Page 17: Outline - Emory University

2. Find fname and lname of the employees who have the most

number of dependents

51

Page 18: Outline - Emory University

3. Find fname and lname of the employee(s) in the ‘Research’

department who earn the highest salary in the department

52

Page 19: Outline - Emory University

3. Find fname and lname of the employee(s) in the ‘Research’

department who earn the highest salary in the department

53

Page 20: Outline - Emory University

4. For each department, show the name, number of employees,

minimum salary and maximum salary paid to the employees in

the department

54

Page 21: Outline - Emory University

4. For each department, show the name, number of employees,

minimum salary and maximum salary paid to the employees in

the department

55

Page 22: Outline - Emory University

Exercise

5. Find fname and lname of all employees who work on more projects than ‘John Smith’

6. Find fname and lname of all employees who work on 2 or more projects controlled by the ‘Research’ department

7. Find fname and lname of all employees who work on all

56

7. Find fname and lname of all employees who work on all projects controlled by the ‘Research’ department

8. Find fname and lname of all employeeds that do not work on any projects controlled by the ‘Research’ department

Page 23: Outline - Emory University

5. Find fname and lname of all employees who work on more

projects than ‘John Smith’

57

Page 24: Outline - Emory University

6. Find fname and lname of all employees who work on 2 or more

projects controlled by the ‘Research’ department

58

Page 25: Outline - Emory University

7. Find fname and lname of all employees who work on all projects controlled by the ‘Research’ department

59

Page 26: Outline - Emory University

8. Find fname and lname of all employeeds that do not work on any projects controlled by the ‘Research’ department

60