Aggregate Functions
Aggregates
• Functions that operate on sets:
– COUNT, SUM, AVG, MAX, MIN
• Produce numbers (not tables)
• Not part of relational algebra
SELECT COUNT(*) SELECT MAX (Salary)
FROM Professor P FROM Employee E
2
Aggregates
Count the number of courses taught in S2000
SELECT COUNT (T.CrsCode)
FROM Teaching T
WHERE T.Semester = ‘S2000’
But if multiple sections of same course
are taught, use:
SELECT COUNT (DISTINCT T.CrsCode)
FROM Teaching T
WHERE T.Semester = ‘S2000’
3
Aggregates: Proper and Improper
Usage
SELECT COUNT (T.CrsCode), T. ProfId
…… --makes no sense (in the absence of
GROUP BY clause)
SELECT COUNT (*), AVG (T.Grade)
…… --but this is OK
WHERE T.Grade > COUNT (SELECT ….
--aggregate cannot be applied to result
of SELECT statement
4
GROUP BY
5
GROUP BY - Example
Transcript
Attributes:
-student’s Id
1234 1234 3.3 4 -avg grade
1234
1234 -number of courses
1234
SELECT T.StudId, AVG(T.Grade), COUNT (*)
FROM Transcript T
GROUP BY T.StudId
6
HAVING Clause
• Eliminates unwanted groups (analogous to WHERE clause)
• HAVING condition constructed from attributes of GROUP BY
list and aggregates of attributes not in list
SELECT T.StudId, AVG(T.Grade) AS CumGpa,
COUNT (*) AS NumCrs
FROM Transcript T
WHERE T.CrsCode LIKE ‘CS%’
GROUP BY T.StudId
HAVING AVG (T.Grade) > 3.5
7
Evaluation of GroupBy with Having
8
Example
• Output the name and address of all seniors
on the Dean’s List
SELECT S.Name, S.Address
FROM Student S, Transcript T
WHERE S.StudId = T.StudId AND S.Status = ‘senior’
S.StudId -- wrong
GROUP BY
S.Name, S.Address -- right
HAVING AVG (T.Grade) > 3.5 AND SUM (T.Credit) > 90
9
ORDER BY Clause
• Causes rows to be output in a specified
order
SELECT T.StudId, COUNT (*) AS NumCrs,
AVG(T.Grade) AS CumGpa
FROM Transcript T
WHERE T.CrsCode LIKE ‘CS%’
GROUP BY T.StudId
HAVING AVG (T.Grade) > 3.5
ORDER BY DESC CumGpa, ASC StudId
10
Query Evaluation Strategy
1 Evaluate FROM: produces Cartesian product, A, of tables
in FROM list
2 Evaluate WHERE: produces table, B, consisting of rows of
A that satisfy WHERE condition
3 Evaluate GROUP BY: partitions B into groups that agree
on attribute values in GROUP BY list
4 Evaluate HAVING: eliminates groups in B that do not
satisfy HAVING condition
5 Evaluate SELECT: produces table C containing a row for
each group. Attributes in SELECT list limited to those in
GROUP BY list and aggregates over group
6 Evaluate ORDER BY: orders rows of C
11