Staffsite STMIK PPKIA Pradnya Paramita DP_8_1
Database Programming with SQL
8-1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Objectives
This lesson covers the following objectives:
• Define and give an example of the seven group functions:
SUM, AVG, COUNT, MIN, MAX, STDDEV, VARIANCE
• Construct and execute a SQL query using group functions
• Construct and execute group functions that operate only with
numeric data types
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
3
Purpose
• What if you were writing an article for the school newspaper
and, to make a point, you wanted to know the average age of
the students at your school?
• What would you have to do to get this information?
• You could ask each student their age in years, months, and
days, add up all of these numbers, and then divide by the
number of students in your school.
• That would be one way -- a very slow and difficult way -- to
find this information.
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
4
Purpose
• What if you needed to know this immediately so that you
could meet a 3:00 p.m. deadline?
• You might have a problem!
• What if each student's date of birth was in a school database
in the STUDENT table?
• It would be so easy then!
• In this lesson, you are going to learn about the power of
group functions in SQL.
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
5
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
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
6
GROUP Functions List
• MIN: Used with columns that
store any data type to return
the minimum value.
• MAX: Used with columns that
store any data type to return
the maximum value.
• SUM: Used with columns that
store numeric data to find the
total or sum of values.
• AVG: Used with columns that
store numeric data to compute
the average.
DPS8L1
Group Functions
DEPT_ID
90
90
90
60
60
60
50
50
50
50
50
…
60
60
10
SALARY
24000
17000
17000
9000
6000
4200
5800
3500
3100
2600
2500
…
11000
8600
7000
4400
SELECT MAX(salary)
FROM employees;
MAX (SALARY)
24000
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
7
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
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
8
GROUP Functions SELECT Clause
• Group functions are written in
the SELECT clause:
SELECT column, group_function(column),
..
FROM table
WHERE condition
GROUP BY column;
• What are Group Functions?
• Group Functions operate on sets
of rows to give one result per
group.
DPS8L1
Group Functions
DEPT_ID
90
90
90
60
60
60
50
50
50
50
50
60
60
60
10
SALARY
24000
17000
17000
9000
6000
4200
5800
3500
3100
2600
2500
10500
11000
8600
7000
4400
The minimum
salary in the
EMPLOYEES
table
MIN (SALARY)
2500
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
9
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
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
10
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)
AS "Lowest Life Exp"
FROM wf_countries;
32.62
SELECT MIN(country_name)
FROM wf_countries;
Anguilla
SELECT MIN(hire_date)
FROM employees;
17/Jun/1987
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
11
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)
AS "Highest Life Exp"
FROM wf_countries;
83.51
SELECT MAX(country_name)
FROM wf_countries
Western Sahara
SELECT MAX(hire_date)
FROM employees;
29/Jan/2000
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
12
GROUP Function examples
• SUM: Used with columns that store numeric data to find the
total or sum of values.
Examples:
Result
SELECT SUM(area)
FROM wf_countries
WHERE region_id = 29;
241424
SELECT SUM(salary)
FROM employees
WHERE department_id = 90;
58000
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
13
GROUP Function examples
• AVG: Used with columns that store numeric data to compute
the average.
Examples:
Result
SELECT AVG(area)
FROM wf_countries
WHERE region_id = 29;
9656.96
SELECT ROUND(AVG(salary), 2)
FROM employees
WHERE department_id = 90;
19333.33
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
14
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 ROUND(VARIANCE(life_expect_at_birth),4)
FROM wf_countries;
143.2394
SELECT ROUND(STDDEV(life_expect_at_birth), 4)
FROM wf_countries;
11.9683
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
15
GROUP Function and NULL
• Group functions ignore NULL values.
• In the example below, the null values
were not used to find the average
commission_pct.
SELECT AVG(commission_pct)
FROM employees;
AVG(COMMISSION_PCT)
.2125
DPS8L1
Group Functions
LAST_NAME
COMMISSION_PCT
King
-
Kochhar
-
De Haan
-
Whalen
-
Higgins
-
Gietz
-
Zlotkey
.2
Abel
.3
Taylor
.2
Grant
.15
Mourgos
-
…
…
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
16
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
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
17
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.
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
18
Terminology
Key terms used in this lesson included:
• AVG
• COUNT
• Group functions
• MAX
• MIN
• STDDEV
• SUM
• VARIANCE
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
19
Summary
In this lesson, you should have learned how to:
• Define and give an example of the seven group functions:
SUM, AVG, COUNT, MIN, MAX, STDDEV, VARIANCE
• Construct and execute a SQL query using group functions
• Construct and execute group functions that operate only with
numeric data types
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
20
8-1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Objectives
This lesson covers the following objectives:
• Define and give an example of the seven group functions:
SUM, AVG, COUNT, MIN, MAX, STDDEV, VARIANCE
• Construct and execute a SQL query using group functions
• Construct and execute group functions that operate only with
numeric data types
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
3
Purpose
• What if you were writing an article for the school newspaper
and, to make a point, you wanted to know the average age of
the students at your school?
• What would you have to do to get this information?
• You could ask each student their age in years, months, and
days, add up all of these numbers, and then divide by the
number of students in your school.
• That would be one way -- a very slow and difficult way -- to
find this information.
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
4
Purpose
• What if you needed to know this immediately so that you
could meet a 3:00 p.m. deadline?
• You might have a problem!
• What if each student's date of birth was in a school database
in the STUDENT table?
• It would be so easy then!
• In this lesson, you are going to learn about the power of
group functions in SQL.
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
5
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
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
6
GROUP Functions List
• MIN: Used with columns that
store any data type to return
the minimum value.
• MAX: Used with columns that
store any data type to return
the maximum value.
• SUM: Used with columns that
store numeric data to find the
total or sum of values.
• AVG: Used with columns that
store numeric data to compute
the average.
DPS8L1
Group Functions
DEPT_ID
90
90
90
60
60
60
50
50
50
50
50
…
60
60
10
SALARY
24000
17000
17000
9000
6000
4200
5800
3500
3100
2600
2500
…
11000
8600
7000
4400
SELECT MAX(salary)
FROM employees;
MAX (SALARY)
24000
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
7
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
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
8
GROUP Functions SELECT Clause
• Group functions are written in
the SELECT clause:
SELECT column, group_function(column),
..
FROM table
WHERE condition
GROUP BY column;
• What are Group Functions?
• Group Functions operate on sets
of rows to give one result per
group.
DPS8L1
Group Functions
DEPT_ID
90
90
90
60
60
60
50
50
50
50
50
60
60
60
10
SALARY
24000
17000
17000
9000
6000
4200
5800
3500
3100
2600
2500
10500
11000
8600
7000
4400
The minimum
salary in the
EMPLOYEES
table
MIN (SALARY)
2500
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
9
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
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
10
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)
AS "Lowest Life Exp"
FROM wf_countries;
32.62
SELECT MIN(country_name)
FROM wf_countries;
Anguilla
SELECT MIN(hire_date)
FROM employees;
17/Jun/1987
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
11
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)
AS "Highest Life Exp"
FROM wf_countries;
83.51
SELECT MAX(country_name)
FROM wf_countries
Western Sahara
SELECT MAX(hire_date)
FROM employees;
29/Jan/2000
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
12
GROUP Function examples
• SUM: Used with columns that store numeric data to find the
total or sum of values.
Examples:
Result
SELECT SUM(area)
FROM wf_countries
WHERE region_id = 29;
241424
SELECT SUM(salary)
FROM employees
WHERE department_id = 90;
58000
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
13
GROUP Function examples
• AVG: Used with columns that store numeric data to compute
the average.
Examples:
Result
SELECT AVG(area)
FROM wf_countries
WHERE region_id = 29;
9656.96
SELECT ROUND(AVG(salary), 2)
FROM employees
WHERE department_id = 90;
19333.33
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
14
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 ROUND(VARIANCE(life_expect_at_birth),4)
FROM wf_countries;
143.2394
SELECT ROUND(STDDEV(life_expect_at_birth), 4)
FROM wf_countries;
11.9683
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
15
GROUP Function and NULL
• Group functions ignore NULL values.
• In the example below, the null values
were not used to find the average
commission_pct.
SELECT AVG(commission_pct)
FROM employees;
AVG(COMMISSION_PCT)
.2125
DPS8L1
Group Functions
LAST_NAME
COMMISSION_PCT
King
-
Kochhar
-
De Haan
-
Whalen
-
Higgins
-
Gietz
-
Zlotkey
.2
Abel
.3
Taylor
.2
Grant
.15
Mourgos
-
…
…
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
16
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
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
17
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.
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
18
Terminology
Key terms used in this lesson included:
• AVG
• COUNT
• Group functions
• MAX
• MIN
• STDDEV
• SUM
• VARIANCE
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
19
Summary
In this lesson, you should have learned how to:
• Define and give an example of the seven group functions:
SUM, AVG, COUNT, MIN, MAX, STDDEV, VARIANCE
• Construct and execute a SQL query using group functions
• Construct and execute group functions that operate only with
numeric data types
DPS8L1
Group Functions
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
20