Week11 Relational Algebra & SQL - Aggregation and Grouping Operation
Week11 Relational Algebra & SQL - Aggregation and Grouping Operation
2
Pearson Education © 2009
Aggregate Operations
AL(R)
– Applies aggregate function list, AL, to R to
define a relation over the aggregate list.
– AL contains one or more
(<aggregate_function>, <attribute>) pairs .
Main aggregate functions are: COUNT, SUM,
AVG, MIN, and MAX.
3
Pearson Education © 2009
Example
4
Example – Aggregate Operations
5
Pearson Education © 2009
Example – Aggregate Operations
Find the average staff salary
R(myAverage) AVERAGE salary (Staff)
6
Grouping Operation
GAAL(R)
– Groups tuples of R by grouping attributes, GA,
and then applies aggregate function list, AL, to
define a new relation.
– AL contains one or more
(<aggregate_function>, <attribute>) pairs.
– Resulting relation contains the grouping
attributes, GA, along with results of each of the
aggregate functions.
7
Pearson Education © 2009
Example – Grouping Operation
Find the number of staff working in each branch and
the sum of their salaries.
R(branchNo, myCount, mySum)
branchNo COUNT staffNo, SUM salary (Staff)
8
Pearson Education © 2009
SQL: SELECT Statement - Aggregates
ISO standard defines five aggregate functions:
9
Pearson Education © 2009
SQL: SELECT Statement - Aggregates
Each operates on a single column of a table and
returns a single value.
COUNT, MIN, and MAX apply to numeric and
non-numeric fields, but SUM and AVG may be
used on numeric fields only.
Apart from COUNT(*), each function eliminates
nulls first and operates only on remaining non-
null values.
10
Pearson Education © 2009
SQL: SELECT Statement - Aggregates
COUNT(*) counts all rows of a table, regardless
of whether nulls or duplicate values occur.
Can use DISTINCT before column name to
eliminate duplicates.
DISTINCT has no effect with MIN/MAX, but
may have with SUM/AVG.
11
Pearson Education © 2009
SQL: SELECT Statement - Aggregates
Aggregate functions can be used only in
SELECT list and in HAVING clause.
12
Pearson Education © 2009
Example : Use of COUNT(*)
How many properties cost more than £350 per
month to rent?
SELECT COUNT(*) AS myCount
FROM PropertyForRent
WHERE rent > 350;
13
Pearson Education © 2009
Example : Use of COUNT(DISTINCT)
How many different properties viewed in May ‘04?
SELECT COUNT(DISTINCT propertyNo) AS myCount
FROM Viewing
WHERE viewDate BETWEEN ‘1-May-04’
AND ‘31-May-04’;
14
Pearson Education © 2009
Example: Use of COUNT and SUM
Find number of Managers and sum of their
salaries.
SELECT COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM Staff
WHERE position = ‘Manager’;
15
Pearson Education © 2009
Example: Use of MIN, MAX, AVG
Find minimum, maximum, and average staff
salary.
SELECT MIN(salary) AS myMin,
MAX(salary) AS myMax,
AVG(salary) AS myAvg
FROM Staff;
16
Pearson Education © 2009
SQL: SELECT Statement - Grouping
Use GROUP BY clause to get sub-totals.
SELECT and GROUP BY closely integrated:
each item in SELECT list must be single-valued
per group, and SELECT clause may only contain:
– column names
– aggregate functions
– constants
– expression involving combinations of the above.
17
Pearson Education © 2009
SQL: SELECT Statement - Grouping
All column names in SELECT list must appear in
GROUP BY clause unless name is used only in an
aggregate function.
If WHERE is used with GROUP BY, WHERE is
applied first, then groups are formed from
remaining rows satisfying predicate.
ISO considers two nulls to be equal for purposes
of GROUP BY.
18
Pearson Education © 2009
Example: Use of GROUP BY
Find number of staff in each branch and their
total salaries.
SELECT branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM Staff
GROUP BY branchNo
ORDER BY branchNo;
19
Pearson Education © 2009
Example : Use of GROUP BY
20
Pearson Education © 2009
Restricted Groupings – HAVING clause
HAVING clause is designed for use with GROUP
BY to restrict groups that appear in final result
table.
Similar to WHERE, but WHERE filters
individual rows whereas HAVING filters groups.
Column names in HAVING clause must also
appear in the GROUP BY list or be contained
within an aggregate function.
21
Pearson Education © 2009
Example : Use of HAVING
For each branch with more than 1 member of
staff, find number of staff in each branch and
sum of their salaries.
SELECT branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM Staff
GROUP BY branchNo
HAVING COUNT(staffNo) > 1
ORDER BY branchNo;
22
Pearson Education © 2009
Example: Use of HAVING
23
Pearson Education © 2009