Docsity

Aggregate Functions - Database Design - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Aggregate Functions - Database Design - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Aggregate Functions

Aggregate Function:

Aggregate functions are functions that take a collection of values as input and return a single value.

 Behavior of Aggregate Functions:

Operates - on a single column Return - a single value. Used only in the SELECT list and in the HAVING clause.

Types of SQL Aggregate Functions

  • SUM
  • AVG
  • MIN
  • MAX
  • COUNT

Input to Aggregate Function

  • SUM and AVG :  Operates only on collections of numbers.
  • MIN , MAX and COUNT  Operates on collection of numeric and non-numeric data types.

Each function eliminates NULL values and operates on Non- null values.

SUM()

Returns: The sum of the values in a specified column.

Example: Find the total/sum of the Managers salary

Query: SELECT SUM( salary) AS sum_salary FROM Staff WHERE Staff.position = ‘Manager’;

Result: sum_salary

AVG()

Returns: The average of the values in a specified column.

Example: Find the average of the Project Managers salary.

Query: SELECT AVG( DISTINCT salary) AS avg_salary FROM Staff WHERE Staff.position = ‘Project Manager’;

Result: avg_salary 10500.00 // Error in Result // avg_salary = 11000. // What is wrong?

Staff

sno fname lname salary position

SL100 John White 30000.00 Manager

SL101 Susan Brand 24000.00 Manager

SL102 David Ford 12000.00 Project Manager

SL103 Ann Beech 12000.00 Project Manager

SL104 Mary Howe 9000.00 Project ManagerDocsity.com

MIN() and MAX()

Returns: MIN() returns the smallest value of a column. MAX() returns the largest value of a column.

Example: Find the minimum and maximum staff salary.

Query: SELECT MIN( salary) AS min_salary, MAX (salary) AS max_salary FROM Staff;

Result: min_salary max_salary 9000.00 30000.

Use of COUNT() and SUM()

sno fname lname salary position

SL100 John White 30000.00 Manager

SL101 Susan Brand 24000.00 Manager

Example: Find the total number of Managers and the sum of there salary.

Query: SELECT COUNT( sno) AS sno_count , SUM(salary) AS sum_salary From Staff WHERE Staff.position = ‘Manager’;

COUNT SUM Docsity.com

COUNT() and SUM() continued

  • Result:

sno_count sum_salary

2 54000.

COUNT(*)

Input: There is no input to this function.

Returns: It counts all the rows of a table , regardless of whether Nulls or the duplicate occur.

Example: How many Project Manager salary is more than

Query: SELECT COUNT(*) AS Count_Salary FROM Staff WHERE Staff.position = ‘Project Manager’ AND Staff.salary > 9000.

COUNT(*) continued…

  • Result:

Count_ Salary

2

Use of GROUP BY

  • GROUP BY: It groups the data from the SELECT table and produce a single summary row for each group
  • When Using GROUP BY:
  1. Each item in the SELECT list must be single valued per group.
  2. SELECT clause may only contain:  Columns names  Aggregate function  Constants  An expression involving combinations of the above.
  3. All column names in SELECT list must appear in the GROUP BY clause unless the name is used only in the aggregate function.

Staff

sno bno fname lname salary position

SL100 B3 John White 30000.00 Manager

SL101 B5 Susan Brand 24000.00 Manager

SL102 B3 David Ford 12000.00 Project Manager

SL103 B5 Ann Beech 12000.00 Project Manager

SL104 B7 Mary Howe 9000.00 Project Manager