Set (or aggregate) functions

  • Set functions:

    • Set function operates on a set of values                

      And returns a singleton set of value

  • Notation of set function:

     γfunc(A)(R) ≡ apply function func on the attr values A in the relation R
    

The Set Functions in Relational Algebra

  • Relational Algebra has the following set functions:

       sum    =  returns the sum     of a set of value
       avg    =  returns the average of a set of value
       max    =  returns the maximum of a set of values
       min    =  returns the maximum of a set of values
       count  =  returns the cardinality of a set (= # elements)
       any    =  returns false if the set is empty, otherwise returns true
    


  • Important note:

    • The return value of a set function is always a set !!!

      Recall:

      • Every operation in Relational Algebra operates on sets and returns a set as result !!!

Examples of Set Functions

  • Suppose relation (= table) employee contains the following tuples (= data):

      employee relation:
        +--------+---------+------+----------+-----+
        | fname  | lname   | sex  | salary   | dno |
        +--------+---------+------+----------+-----+
        | John   | Smith   | M    | 30000.00 |   5 |
        | Frankl | Wong    | M    | 40000.00 |   5 |
        | Alicia | Zelaya  | F    | 20000.00 |   4 |
        | Jennif | Wallace | F    | 50000.00 |   4 |
        +--------+---------+------+----------+-----+   
    

    Then:

    • γsum(salary)(employee) = {140000}       
    • γavg(salary)(employee) = {35000}
    • γmax(salary)(employee) = {50000}
    • γmin(salary)(employee) = {20000}
    • γcount( * )(employee) = {4}                                   ( * means: a tuple)
    • γany( * )(employee) = {true}

How to use set functions

  • Query:

    • Find the highest salary earned by employees in department number 5

    Solution:

      Sample employee relation: (to make solution concrete)
    
           fname  lname    dno         salary    
           ------ -------- ----------- --------- 
           John   Smith              5  30000.00 
           Frankl Wong               5  40000.00 
           Alicia Zelaya             4  25000.00 
           Jennif Wallace            4  43000.00 
           Ramesh Narayan            5  38000.00 
           Joyce  English            5  25000.00 
           Ahmad  Jabbar             4  25000.00 
           James  Borg               1  55000.00       
    
    
    
    
    
    
    

How to use set functions

  • Query:

    • Find the highest salary earned by employees in department number 5

    Step 1: find the employees in department 5:

      σdno=5( employee )
    
           fname  lname    dno         salary    
           ------ -------- ----------- --------- 
           John   Smith              5  30000.00 
           Frankl Wong               5  40000.00 
           Alicia Zelaya             4  25000.00
           Jennif Wallace            4  43000.00 
           Ramesh Narayan            5  38000.00 
           Joyce  English            5  25000.00 
           Ahmad  Jabbar             4  25000.00 
           James  Borg               1  55000.00       
    
    
    
    
    
    
    

How to use set functions

  • Query:

    • Find the highest salary earned by employees in department number 5

    Step 2: find the maximum of the "salary" attribute value in this set:

      σdno=5( employee )
    
           fname  lname    dno         salary    
           ------ -------- ----------- --------- 
           John   Smith              5  30000.00 
           Frankl Wong               5  40000.00 
           Alicia Zelaya             4  25000.00
           Jennif Wallace            4  43000.00 
           Ramesh Narayan            5  38000.00 
           Joyce  English            5  25000.00 
           Ahmad  Jabbar             4  25000.00 
           James  Borg               1  55000.00       
    
      γmax(salary)( σdno=5( employee ) ): 
    
             max     <=== Attribute name    
             --------- 
              40000.00 
    

How to use set functions in a relational algebra query

  • Query:

    • Find the employee(s) who earn the highest salary in the company

    Solution:

      Sample employee relation: (to make solution concrete)
    
           fname  lname    dno         salary    
           ------ -------- ----------- --------- 
           John   Smith              5  30000.00 
           Frankl Wong               5  40000.00 
           Alicia Zelaya             4  25000.00 
           Jennif Wallace            4  43000.00 
           Ramesh Narayan            5  38000.00 
           Joyce  English            5  25000.00 
           Ahmad  Jabbar             4  25000.00 
           James  Borg               1  55000.00       
    
    
    
    
    
    
    

How to use set functions in a relational algebra query

  • Query:

    • Find the employee(s) who earn the highest salary in the company

    Step 1: First, we find the highest salary paid:

      Sample employee relation: (to make solution concrete)
    
           fname  lname    dno         salary    
           ------ -------- ----------- --------- 
           John   Smith              5  30000.00 
           Frankl Wong               5  40000.00 
           Alicia Zelaya             4  25000.00 
           Jennif Wallace            4  43000.00 
           Ramesh Narayan            5  38000.00 
           Joyce  English            5  25000.00 
           Ahmad  Jabbar             4  25000.00 
           James  Borg               1  55000.00       
    
      M =  γmax(salary)( employee )                M:   max
                                                     ----------
    					          55000.00
    
    
    

How to use set functions in a relational algebra query

  • Query:

    • Find the employee(s) who earn the highest salary in the company

    Step 2: then, we find the employee(s) that earn the highest salary:

      Sample employee relation: (to make solution concrete)
    
           fname  lname    dno         salary    
           ------ -------- ----------- --------- 
           John   Smith              5  30000.00 
           Frankl Wong               5  40000.00 
           Alicia Zelaya             4  25000.00 
           Jennif Wallace            4  43000.00 
           Ramesh Narayan            5  38000.00 
           Joyce  English            5  25000.00 
           Ahmad  Jabbar             4  25000.00 
           James  Borg               1  55000.00       
    
      M =  γmax(salary)( employee )                M:   max
                                                     ----------
    					          55000.00
    
      πfn,ln( Employee salary=max M )  ---> James  Borg