0% found this document useful (0 votes)
4 views16 pages

Functions-2 DBMS

Aggregate functions process a collection of values to return a single value, operating on a single column and used in SELECT lists and HAVING clauses. Functions like SUM, AVG, MIN, MAX, and COUNT are detailed, with examples demonstrating their usage on a staff dataset, including handling of NULL values and the distinction between DISTINCT and ALL. The document provides SQL queries to illustrate how to calculate totals, averages, and counts based on specific conditions.

Uploaded by

sourabh.planetc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

Functions-2 DBMS

Aggregate functions process a collection of values to return a single value, operating on a single column and used in SELECT lists and HAVING clauses. Functions like SUM, AVG, MIN, MAX, and COUNT are detailed, with examples demonstrating their usage on a staff dataset, including handling of NULL values and the distinction between DISTINCT and ALL. The document provides SQL queries to illustrate how to calculate totals, averages, and counts based on specific conditions.

Uploaded by

sourabh.planetc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

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.
BEHAVIOR OF AGGREGATE
FUNCTION CONTINUED…
Accepts:

 DISTINCT : consider only distinct values of the


argument expression.

 ALL : consider all values including all


duplicates.

Example: SELECT COUNT( DISTINCT column_name)


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.
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
Manager
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 position = ‘Manager’;

Result:
sum_salary
54000.00
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 position = ‘Project Manager’;

Result:
avg_salary
10500.00 // Error in Result
// avg_salary = 11000.00
// What is wrong?
REVISED QUERY FOR AVG()

Query:

SELECT AVG(ALL salary) AS avg_salary


FROM Staff
WHERE position = ‘Project Manager’;

Result :
avg_salary
11000.00

CAUTION: Using DISTINCT and ALL in SUM() and AVG()


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
Manager
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.00
COUNT()
Returns: The number of values in the specified column.

Example: Count number of staffs who are Manager.

Query: SELECT COUNT(sno) AS sno_count


FROM Staff
WHERE position = ‘Manager’;
Result:
sno_count
2
USE OF COUNT() AND SUM()
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 position = ‘Manager’;
sno fname lname salary position

SL100 John White 30000.00 Manager

SL101 Susan Brand 24000.00 Manager

COUNT SUM
COUNT() AND SUM() CONTINUED
 Result:

sno_count sum_salary
2 54000.00
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
Manager
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


9000.00

Query: SELECT COUNT(*) AS Count_Salary


FROM Staff
WHERE position = ‘Project Manager’ AND
salary > 9000.00
COUNT(*) CONTINUED…
 Result:

Count_ Salary
2

You might also like