(It) 12 Aggregate Function
(It) 12 Aggregate Function
txt 1
AGGREGAT
E
FUNCTION Maxmanroe.com
Learning Outcomes
Students are able to use aggregate function
3
Overview
AGGREGAT
E
FUNCTION
Part 1.1 Overview
Maxmanroe.com
Overview
• Oftentimes, we're also interested in summarizing our data to determine trends or
produce top-level reports.
• Fortunately, SQL provides aggregate functions to assist with the summarization of
large volumes of data. In this three-segment article, we'll look at functions that
allow us to add and average data, count records meeting specific criteria and find
the largest and smallest values in a table.
• In computer science, an aggregate function is a function that returns a single value
from a collection of input values.
6
AGGREGAT
E
FUNCTION
Part 1.2 AVG and SUM Clause
Maxmanroe.com
You can use AVG and SUM functions against columns that can store
numeric data.
9
AGGREGAT
E
FUNCTION
Part 1.3 MIN and MAX Clause
Maxmanroe.com
You can use the MAX and MIN functions for any data type.
AGGREGAT
E
FUNCTION
Part 1.4 COUNT Clause
Maxmanroe.com
SELECT COUNT(*)
FROM employees
WHERE department_id = 50;
13
AGGREGAT
E
FUNCTION
Part 1.5 NVL Clause
Maxmanroe.com
SELECT AVG(commission_pct)
FROM employees;
17
AGGREGAT
E
FUNCTION
Part 1.6 GROUP BY Clauses
Maxmanroe.com
9500 The
average
3500
salary
in
EMPLOYEES
6400
table
for each
department.
10033
…
20
GROUP BY Clauses
• GROUP BY is particularly useful when paired with aggregate
functions
• GROUP BY divides a table into subsets (by groups)
• An aggregate function can be used to provide summary information for that group.
• Scalar Aggregate
• A single value returned from an SQL query that includes an aggregate function.
• Vector Aggregate
• Multiple values returned from an SQL query that includes an aggregate function.
21
GROUP BY Clauses
• Count the number of customers with
addresses in each state to which we ship.
“Add up the
salaries in
the EMPLOYEES
table
for each job,
grouped by
department.
…
24
Illegal Queries
Using Group Functions
• Any column or expression in the SELECT list that is not an
aggregate function must be in the GROUP BY clause.
SELECT
SELECT department_id,
department_id, COUNT(last_name)
COUNT(last_name)
FROM
FROM employees;
employees;
SELECT
SELECT department_id,
department_id, COUNT(last_name)
COUNT(last_name)
**
ERROR
ERROR at
at line
line 1:
1:
ORA-00937:
ORA-00937: not
not aa single-group
single-group group
group function
function
Illegal Queries
Using Group Functions
• You cannot use the WHERE clause to restrict groups.
• You cannot use group functions in the WHERE clause.
27
Illegal Queries
Using Group Functions
SELECT
SELECT department_id,
department_id, AVG(salary)
AVG(salary)
FROM
FROM employees
employees
WHERE
WHERE AVG(salary)
AVG(salary) >> 8000
8000
GROUP
GROUP BY
BY department_id;
department_id;
WHERE
WHERE AVG(salary)
AVG(salary) >> 8000
8000
**
ERROR
ERROR at
at line
line 3:
3:
ORA-00934:
ORA-00934: group
group function
function is
is not
not allowed
allowed here
here
Cannot use the WHERE clause to restrict groups
28
AGGREGAT
E
FUNCTION
Part 1.7 HAVING Clause
Maxmanroe.com
The maximum
salary
per department
when it is
greater than
$10,000
…
30
HAVING Clauses
• The HAVING clause acts like a WHERE clause, but it identifies
groups, rather than rows, that meet a criterion.
• Therefore, you will usually see a HAVING clause following a
GROUP BY clause.
31
HAVING Clauses
• Find only states with more than one
customer.
HAVING Clauses
SELECT job_id, SUM(salary) PAYROLL
FROM employees
WHERE job_id NOT LIKE '%REP%'
GROUP BY job_id
HAVING SUM(salary) > 13000
ORDER BY SUM(salary);
34
AGGREGAT
E
FUNCTION
Part 1.8 Nested Function
Maxmanroe.com
SELECT MAX(AVG(salary))
FROM employees
GROUP BY department_id;
36
PROCESSIN
G SINGLE
TABLE
Part 1.9 Example of Basic Queries
Maxmanroe.com
References
Hoffer, Jeffrey A., et.al., "Modern Database Management", Twelfth
Edition, Pearson, 2016. Chapter 6.
Recommended Link:
https://www.sqltutorial.org/
41
THANK
YOU