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

Database Management Concepts Lab Course Code: CSE 704: Group Functions

This document discusses SQL group functions. It defines group functions as functions that operate on sets of rows to give one result per group. The group functions covered are AVG, COUNT, MAX, MIN, SUM, VARIANCE, and STDDEV. Examples are provided for each function showing how they can be used in the SELECT clause to aggregate data and return a single value. Cautions discussed include that group functions cannot be used in the WHERE clause and that some functions only work on numeric data types.

Uploaded by

Naidul H Digonto
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)
48 views16 pages

Database Management Concepts Lab Course Code: CSE 704: Group Functions

This document discusses SQL group functions. It defines group functions as functions that operate on sets of rows to give one result per group. The group functions covered are AVG, COUNT, MAX, MIN, SUM, VARIANCE, and STDDEV. Examples are provided for each function showing how they can be used in the SELECT clause to aggregate data and return a single value. Cautions discussed include that group functions cannot be used in the WHERE clause and that some functions only work on numeric data types.

Uploaded by

Naidul H Digonto
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

Database Management Concepts Lab

Course Code: CSE 704

Lecture 6
Topic
Group Functions
GROUP Functions
• In SQL, the following group functions can operate on a
whole table or on a specific grouping of rows. Each
function returns one result.
• Group Functions:
– AVG
– COUNT
– MIN
– MAX
– SUM
– VARIANCE
– STDDEV
GROUP Functions List
• MIN: Used with columns that store any DEPT_ID SALARY SELECT MAX(salary)
FROM employees;
90 24000
data type to return the minimum value. 90 17000
90 17000
• MAX: Used with columns that store any 60 9000
data type to return the maximum value. 60 6000
60 4200
• SUM: Used with columns that store 50 5800
MAX (SALARY)
50 3500
numeric data to find the total or sum of 50 3100 24000
values. 50 2600
50 2500
• AVG: Used with columns that store … …
numeric data to compute the average. 60 11000
60 8600
7000
SQL> SELECT dept, AVG(NVL(sal,0))
10 4400
FROM employee;
GROUP Functions List
• COUNT: Returns the number of rows.
• VARIANCE: Used with columns that store numeric data to
calculate the spread of data around the mean. For example, if the
average grade for the class on the last test was 82% and the
student's scores ranged from 40% to 100%, the variance of scores
would be greater than if the student's scores ranged from 78% to
88%.
• STDDEV: Similar to variance, standard deviation measures the
spread of data. For two sets of data with approximately the same
mean, the greater the spread, the greater the standard deviation
GROUP Functions SELECT Clause
DEPT_ID SALARY
• Group functions are written 90 24000
90 17000 The minimum salary in
in the SELECT clause: 90 17000 the EMPLOYEES
60 9000 table
SELECT column, group_function(column),
.. 60 6000
FROM table 60 4200
WHERE condition 50 5800
GROUP BY column; 50 3500 MIN
(SALARY)
50 3100
2500
• What are Group Functions? 50
50
2600
2500

• Group Functions operate on 60


60
10500
11000
sets of rows to give one 60 8600
result per group. 10
7000
4400
GROUP Function Cautions
• Important things you should know about group
functions:
– Group functions cannot be used in the WHERE clause:
SELECT last_name, first_name
FROM employees
WHERE salary = MIN(salary);

ORA-00934: group function is not allowed


here
GROUP Function examples
• MIN: Used with columns that store any data type to
return the minimum value.
Examples: Result
SELECT MIN(life_expect_at_birth) 32.62
AS "Lowest Life
Exp" FROM
wf_countries;
SELECT Anguilla
MIN(country_name)
FROM wf_countries;
SELECT 17/Jun/1987
MIN(hire_date)
FROM employees;
GROUP Function examples
• MAX: Used with columns that store any data type to
return the maximum value.
Examples: Result
SELECT MAX(life_expect_at_birth) 83.51
AS "Highest Life
Exp" FROM
wf_countries;
SELECT Western Sahara
MAX(country_name)
FROM wf_countries
SELECT 29/Jan/2000
MAX(hire_date)
FROM employees;
GROUP Function examples
• SUM: Used with columns that store numeric data to
find the total or sum of values.
Examples: Result
SELECT SUM(area) 241424
FROM wf_countries
WHERE region_id =
29;
SELECT SUM(salary) 58000
FROM employees
WHERE department_id = 90;
GROUP Function examples
• AVG: Used with columns that store numeric data to
compute the average.
Examples: Result
SELECT AVG(area) 9656.96
FROM wf_countries
WHERE region_id =
29;
SELECT ROUND(AVG(salary), 2) 19333.33
FROM employees
WHERE department_id = 90;
GROUP Function examples
• VARIANCE: Used with columns that store numeric
data to calculate the spread of data around the
mean.
• STDDEV: Similar to variance, standard deviation
measures the spread of data.
Examples: Result
SELECT 143.2394
ROUND(VARIANCE(life_expect_at_birth),4)
FROM wf_countries;
SELECT 11.9683
ROUND(STDDEV(life_expect_at_birth), 4)
FROM wf_countries;
GROUP Function and NULL
• Group functions ignore NULL
values. LAST_NAME COMMISSION_P
• In the example below, the null King
CT
-
values were not used to find the Kochhar -
average commission_pct. De Haan -
Whalen -
SELECT AVG(commission_pct) Higgins -
FROM employees;
Gietz -

AVG(COMMISSION_PCT) Zlotkey .2
Abel .3
.2125
Taylor .2
Grant .15
Mourgos -
… …
More Than One Group Function
• You can have more than one group function in the
SELECT clause, on the same or different columns.
SELECT MAX(salary), MIN(salary), MIN(employee_id)
FROM employees
WHERE department_id = 60;

MAX(SALARY) MIN(SALARY) MIN(EMPLOYEE_ID)

9000 4200 103


Rules for Group Functions
• Group functions ignore null values.
• Group functions cannot be used in the WHERE clause.
• MIN, MAX and COUNT can be used with any data type;
SUM, AVG, STDDEV, and VARIANCE can be used only
with numeric data types.
SELECT AVG(MAX(salary)) FROM employees
GROUP BY department_id;
Result
AVG(MAX(SALARY)) 10926.3333
Terminology
Key terms used in this lesson
included:
• AVG
• COUNT
• Group functions
• MAX
• MIN
• STDDEV
• SUM
• VARIANCE
Thank you

You might also like