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 ([Link])
FROM Teaching T
WHERE [Link] = ‘S2000’
But if multiple sections of same course
are taught, use:
SELECT COUNT (DISTINCT [Link])
FROM Teaching T
WHERE [Link] = ‘S2000’
3
Aggregates: Proper and Improper
Usage
SELECT COUNT ([Link]), T. ProfId
…… --makes no sense (in the absence of
GROUP BY clause)
SELECT COUNT (*), AVG ([Link])
…… --but this is OK
WHERE [Link] > 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 [Link], AVG([Link]), COUNT (*)
FROM Transcript T
GROUP BY [Link]
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 [Link], AVG([Link]) AS CumGpa,
COUNT (*) AS NumCrs
FROM Transcript T
WHERE [Link] LIKE ‘CS%’
GROUP BY [Link]
HAVING AVG ([Link]) > 3.5
7
Evaluation of GroupBy with Having
8
Example
• Output the name and address of all seniors
on the Dean’s List
SELECT [Link], [Link]
FROM Student S, Transcript T
WHERE [Link] = [Link] AND [Link] = ‘senior’
[Link] -- wrong
GROUP BY
[Link], [Link] -- right
HAVING AVG ([Link]) > 3.5 AND SUM ([Link]) > 90
9
ORDER BY Clause
• Causes rows to be output in a specified
order
SELECT [Link], COUNT (*) AS NumCrs,
AVG([Link]) AS CumGpa
FROM Transcript T
WHERE [Link] LIKE ‘CS%’
GROUP BY [Link]
HAVING AVG ([Link]) > 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