0% found this document useful (0 votes)
251 views

Week11 Relational Algebra & SQL - Aggregation and Grouping Operation

Description

Uploaded by

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

Week11 Relational Algebra & SQL - Aggregation and Grouping Operation

Description

Uploaded by

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

Week 11

Relational Algebra and SQL


(Aggregation and Grouping Operation)

Pearson Education © 2009


Objectives
 RelationalAlgebra on aggregation and
grouping operation.

 SQL on aggregation and grouping.

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

 How many staff earn more than £10,000?

R(myCount) COUNT staffNo (σsalary > 10000 (Staff))

5
Pearson Education © 2009
Example – Aggregate Operations
 Find the average staff salary
R(myAverage) AVERAGE salary (Staff)

6
Grouping Operation
 GAAL(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:

COUNT returns number of values in specified


column.
SUM returns sum of values in specified column.
AVG returns average of values in specified column.
MIN returns smallest value in specified column.
MAX returns largest value in specified column.

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.

 If SELECT list includes an aggregate function


and there is no GROUP BY clause, SELECT list
cannot reference a column out with an aggregate
function. For example, the following is illegal:
SELECT staffNo, COUNT(salary)
FROM Staff;

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

You might also like