0% found this document useful (0 votes)
35 views41 pages

(It) 12 Aggregate Function

The document discusses aggregate functions in SQL, including AVG, SUM, MIN, MAX, COUNT, NVL, and GROUP BY clauses. It provides examples of how to use each function to summarize and analyze data from tables, including grouping and filtering query results.

Uploaded by

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

(It) 12 Aggregate Function

The document discusses aggregate functions in SQL, including AVG, SUM, MIN, MAX, COUNT, NVL, and GROUP BY clauses. It provides examples of how to use each function to summarize and analyze data from tables, including grouping and filtering query results.

Uploaded by

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

11 quiz review.

txt 1

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

AGGREGAT
E
FUNCTION Maxmanroe.com

Image Designed by stories / Freepik


2

Learning Outcomes
Students are able to use aggregate function
3

Overview

Function Type and Syntax

• AVG and SUM


• MIN and MAX
• COUNT
• NVL
• GROUP BY
• HAVING
• NESTED FUNTION
4

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

AGGREGAT
E
FUNCTION
Part 1.1 Overview
Maxmanroe.com

Image Designed by stories / Freepik


5

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

Group Functions Syntax


SELECT [column,] group_function(column), ...
FROM table
[WHERE condition]
[GROUP BY column]
[ORDER BY column];
7

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

AGGREGAT
E
FUNCTION
Part 1.2 AVG and SUM Clause
Maxmanroe.com

Image Designed by stories / Freepik


8

Using the AVG and SUM Functions


• You can use AVG and SUM for numeric data.
SELECT AVG(salary), MAX(salary),
MIN(salary), SUM(salary)
FROM employees
WHERE job_id LIKE '%REP%';

You can use AVG and SUM functions against columns that can store
numeric data.
9

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

AGGREGAT
E
FUNCTION
Part 1.3 MIN and MAX Clause
Maxmanroe.com

Image Designed by stories / Freepik


10

Using the MIN and MAX Functions


• You can use MIN and MAX for any data type.
SELECT MIN(hire_date), MAX(hire_date)
FROM employees;

You can use the MAX and MIN functions for any data type.

SELECT MIN(last_name), MAX(last_name)


FROM employees;
11

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

AGGREGAT
E
FUNCTION
Part 1.4 COUNT Clause
Maxmanroe.com

Image Designed by stories / Freepik


12

Using the COUNT Function


• Using the COUNT Function

SELECT COUNT(*)
FROM employees
WHERE department_id = 50;
13

Using the COUNT Function


• COUNT(expr) returns the number of rows with non-null values
for the expr.
• Display the number of department values in the EMPLOYEES
table, excluding the null values.
SELECT COUNT(commission_pct)
FROM employees
WHERE department_id = 80;
14

Using the COUNT Function


• COUNT(DISTINCT expr) returns the number of distinct non-
null values of the expr.
• Display the number of distinct department values in the
EMPLOYEES table.
SELECT COUNT(DISTINCT department_id)
FROM employees;
15

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

AGGREGAT
E
FUNCTION
Part 1.5 NVL Clause
Maxmanroe.com

Image Designed by stories / Freepik


16

Group Functions and Null Values


• Group functions ignore null values in the column.

SELECT AVG(commission_pct)
FROM employees;
17

Using the NVL Function with Group Functions

• The NVL function forces group functions to include null values.

SELECT AVG(NVL(commission_pct, 0))


FROM employees;
18

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

AGGREGAT
E
FUNCTION
Part 1.6 GROUP BY Clauses
Maxmanroe.com

Image Designed by stories / Freepik


19

Creating Groups of Data


EMPLOYEES
4400

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

Creating Groups of Data:


The GROUP BY Clause Syntax
SELECT column, group_function(column)
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[ORDER BY column];

Divide rows in a table into smaller groups by


using the
GROUP BY clause.
22

GROUP BY Clauses
• Count the number of customers with
addresses in each state to which we ship.

SELECT CustomerState, COUNT (CustomerState)


FROM Customer_T
GROUP BY CustomerState;
23

Grouping by More Than One Column


EMPLOYEES

“Add up the
salaries in
the EMPLOYEES
table
for each job,
grouped by
department.

24

Using the GROUP BY Clause


on Multiple Columns
SELECT department_id dept_id, job_id, SUM(salary)
FROM employees
GROUP BY department_id, job_id ;
25

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

Column missing in the GROUP BY clause


26

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

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

AGGREGAT
E
FUNCTION
Part 1.7 HAVING Clause
Maxmanroe.com

Image Designed by stories / Freepik


29

Excluding Group Results


EMPLOYEES

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 Clause Syntax

SELECT column, group_function


FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
32

HAVING Clauses
• Find only states with more than one
customer.

SELECT CustomerState, COUNT (CustomerState)


FROM Customer_T
GROUP BY CustomerState
HAVING COUNT (CustomerState) > 1;
33

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

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

AGGREGAT
E
FUNCTION
Part 1.8 Nested Function
Maxmanroe.com

Image Designed by stories / Freepik


35

Nesting Group Functions


• Display the maximum average salary.

SELECT MAX(AVG(salary))
FROM employees
GROUP BY department_id;
36

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G SINGLE
TABLE
Part 1.9 Example of Basic Queries
Maxmanroe.com

Image Designed by stories / Freepik


37

Example of Basic Queries-5


• List, in alphabetical order, the product finish and the average Product_T
standard price for each finish having an average standard ProductID
price less than $750. ProductDescription
ProductFinish
ProductStandardPrice
ProductCost
ProductPriorYearGoal
ProductCurrentYearGoal
ProductLineID
38

Example of Basic Queries-5


• List, in alphabetical order, the product finish and the average Product_T
standard price for each finish having an average standard ProductID
price less than $750. ProductDescription
ProductFinish
• order by ProductFinish ProductStandardPrice
• group by ProductFinish  avg (ProductStandardPrice) ProductCost
• having avg (ProductStandardPrice) < 750 ProductPriorYearGoal
ProductCurrentYearGoal
ProductLineID
39

Example of Basic Queries-5


• List, in alphabetical order, the product finish and the average Product_T
standard price for each finish having an average standard ProductID
price less than 750. ProductDescription
ProductFinish
ProductStandardPrice
ProductCost
SELECT ProductFinish, AVG (ProductStandardPrice) ProductPriorYearGoal
FROM Product_T ProductCurrentYearGoal
GROUP BY ProductFinish ProductLineID
HAVING AVG (ProductStandardPrice) < 750
ORDER BY ProductFinish;
40

References
Hoffer, Jeffrey A., et.al., "Modern Database Management", Twelfth
Edition, Pearson, 2016. Chapter 6.

Recommended Link:
https://www.sqltutorial.org/
41

THANK
YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy