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

10.2 - Chapter 6 - Full Relations Operations

This document discusses SQL grouping and aggregate functions. It covers: 1. Aggregate functions like AVG, COUNT, MAX, MIN, SUM which operate on groups of rows and give a single result. 2. The GROUP BY clause which divides rows into groups based on specified columns. 3. How NULL values are treated when grouping - they are ignored by aggregate functions but can be used to form groups. 4. The HAVING clause which allows filtering groups based on conditions, after rows have been grouped. Examples demonstrate using aggregate functions, GROUP BY, and HAVING together.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views22 pages

10.2 - Chapter 6 - Full Relations Operations

This document discusses SQL grouping and aggregate functions. It covers: 1. Aggregate functions like AVG, COUNT, MAX, MIN, SUM which operate on groups of rows and give a single result. 2. The GROUP BY clause which divides rows into groups based on specified columns. 3. How NULL values are treated when grouping - they are ignored by aggregate functions but can be used to form groups. 4. The HAVING clause which allows filtering groups based on conditions, after rows have been grouped. Examples demonstrate using aggregate functions, GROUP BY, and HAVING together.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

ĐẠI HỌC FPT CẦN THƠ

Chapter 6
Full relation operations
Objectives

1 Understand what are grouping and aggregate functions

2 Understand HAVING clauses

3 Know about the NULL when grouping


Contents

1 Aggregation operators

2 Grouping

3 Grouping, Aggregation and NULLs

4 HAVING clauses
1 - Aggregate Functions
OR Aggregate Operators

STUDENT
CLASS_ID ID NAME
106 1A
106 2B
107 3C
107 4D
5E
6F
7G
8H

Group functions (or Aggregate Functions) operate on sets of


rows to give one result per group. These sets may be the
whole table or the table split into groups.
Exp: If we want all rows with the same CLASS_ID value will
be groupped: GROUP BY class_id
1 - Aggregate Functions
OR Aggregate Operators
AVG
COUNT
MAX
MIN
SUM
STDDEV
VARIANCE

 DISTINCT makes the function consider only nonduplicate values;


 ALL makes it consider every value including duplicates.
 The default is ALL and therefore does not need to be specified.
 All group functions ignore NULL values.
Example: AVG, SUM, MIN, MAX, COUNT
Example: Aggregate Functions
and DISTINCT
2 - Grouping

Until now, all group functions (demonstrated in above


examples) have treated the table as one large group
of information.
At times, you need to divide the table of information
into smaller groups. This can be done by using the
GROUP BY clause
The keyword GROUP BY is followed by a list of
grouping attributes.
2 - Grouping

Syntax: Divide rows in a table into smaller


groups by using the GROUP BY clause.

SELECT column, group_function (column)


FROM table
[WHERE conditions]
[GROUP BY group_by_expression]
[ORDER BY {column [ASC | DESC] ,…} ]

group_by_expression: specifies columns whose


values determine the basis for grouping rows
Example: Aggregate Functions
and GROUP BY
Example: Aggregate Functions
and GROUP BY
Example: Aggregate Functions
and GROUP BY
2 – Grouping: rules to remember

SELECT column, group_function (column)


FROM table
[WHERE conditions]
[GROUP BY group_by_expression]
[ORDER BY {column [ASC | DESC] ,…} ]

When aggregate functions are used in a select


list, the select list can contain only:
• Aggregate functions
• Grouping columns from a GROUP BY clause
• An expression that returns the same value for every
row in the result set, such as a constant
6.4.6 – Grouping, Aggregation,
and NULLs
When tuples have nulls, there are a few rules we must
remember:
The value NULL is ignored in any aggregation: it does
not contribute to a SUM, AVG or COUNT of an attribute, nor
can it be the minimum or maximum in its column.
Exp: COUNT(*) is always a count of the number of tuples in
a relation; but COUNT(A) is the number of tuples with non-
NULL values for attribute A
On the other hand, NULL is treated as an ordinary
value when forming groups: that is, we can have a
group in which one or more of the grouping attributes are
assigned the value NULL
When we perform any aggregation except COUNT over an
empty bag of values, the result is NULL (the COUNT of an
empty bag is 0)
3 - HAVING clauses

Sometimes, we want to exclude some groups from displaying


result. The solution is using HAVING clause
3 - HAVING clauses

Syntax:
SELECT column, group_function (column)
FROM table
[WHERE conditions]
[GROUP BY group_by_expression]
[HAVING conditions]
[ORDER BY {column [ASC | DESC] ,…} ]

 The WHERE clause is used to restrict the rows that


you select
 But the HAVING clause is used to restrict groups.
6.4.7 - HAVING clauses
(page 288)
SELECT column, group_function (column)
FROM table
[WHERE conditions]
[GROUP BY group_by_expression]
[HAVING conditions]
[ORDER BY {column [ASC | DESC] ,…} ]

Groups are formed and group functions are calculated


before the HAVING clause is applied to the groups.
In Oracle (not SQL Server), the HAVING clause can
precede the GROUP BY clause, but it is recommended
that you place the GROUP BY clause first because it is
more logical.
3 - HAVING clauses

1
Select ROWS (with WHERE clause) first

2
ROWS are grouped (GROUP BY clause)

3
Groups matching the HAVING clause are
displayed
Example: HAVING

The example displays department numbers and


maximum salaries for those departments whose
maximum salary is greater than $10,000.
Example: HAVING

The example displays the job ID and total


monthly salary for each job with a total
payroll exceeding $13,000.
Example: HAVING

Group functions can be nested to a depth of


two. The example displays the maximum
average salary.
ĐẠI HỌC FPT CẦN THƠ

You might also like