Sub topic 8
Grouping and aggregation in relational algebra
This chapter assumes you know selection, projection, joins and renaming in relational algebra. It also assumes you know the basic SQL aggregates COUNT, SUM, AVG, MIN and MAX. Basic relational algebra cannot count anything. This chapter adds one operator that can, and it shows how that operator maps onto SQL's GROUP BY and HAVING.
Why the basic operators are not enough
Selection, projection, union, difference, product and rename all return tuples that already exist in, or are combined from, the input. None of them can produce a new value such as "the number of students in course 10". Extended relational algebra adds an operator that collapses many tuples into one summary tuple.
The grouping and aggregation operator
The operator is written with the Greek letter gamma, or with a script G in many textbooks. Both notations mean the same thing:
γ_{g1, g2, ... ; F1(a1) → x1, F2(a2) → x2, ...}(R)
or equivalently
_{g1, g2, ...} 𝒢 _{F1(a1), F2(a2), ...}(R)g1, g2, ...are the grouping attributes. Tuples that agree on all of them go into the same group.F(a)is an aggregate function applied to attributeainside each group.→ xoptionally names the result column.
The result has one tuple per group. Its attributes are the grouping attributes followed by the aggregate results. Every other attribute of R is dropped.
The aggregate functions are COUNT, SUM, AVG, MIN and MAX.
A worked example
Use Student(sid, sname, age) and Enrolled(sid, cid) with this data in Enrolled:
| sid | cid |
|---|---|
| 1 | 10 |
| 1 | 20 |
| 2 | 10 |
| 3 | 10 |
How many students are in each course?
γ_{cid; COUNT(sid) → num_students}(Enrolled)The tuples split into two groups by cid:
| cid | num_students |
|---|---|
| 10 | 3 |
| 20 | 1 |
The number of tuples in the result equals the number of distinct values of the grouping attribute, here 2. This fact is the basis of many GATE counting questions.
The no-grouping form
If you leave out the grouping attributes, the whole relation is one group:
γ_{COUNT(sid) → total, MAX(cid) → top}(Enrolled)This returns exactly one tuple, (4, 20). Without grouping, even an empty input produces one tuple. With grouping, an empty input produces no tuples, because there are no groups.
Where WHERE and HAVING go
Algebra has no separate keywords. The order of operators carries the meaning.
- A condition applied before grouping (SQL
WHERE) is a selection inside theγ. - A condition on an aggregate (SQL
HAVING) is a selection outside theγ.
Courses with at least 3 enrollments, counting only students older than 20:
π_{cid}( σ_{n >= 3}( γ_{cid; COUNT(Student.sid) → n}( σ_{age > 20}(Student ⋈ Enrolled) ) ) )The diagram shows the evaluation order.
The SQL form of the same query is below.
SELECT e.cid
FROM Student s
JOIN Enrolled e ON s.sid = e.sid
WHERE s.age > 20
GROUP BY e.cid
HAVING COUNT(s.sid) >= 3;Semantics worth memorizing
Duplicates
Plain relational algebra uses sets, but aggregation only makes sense over bags (multisets). SUM over ages {20, 20, 21} is 61, not 41. When a textbook or GATE question uses γ, assume duplicates inside a group are counted unless the question says COUNT(DISTINCT ...).
NULLs
Aggregates other than COUNT(*) ignore NULL inputs. If a group's age values are 18, 21, NULL:
| Expression | Result |
|---|---|
| `COUNT(*)` | 3 |
| `COUNT(age)` | 2 |
| `AVG(age)` | 19.5 |
| `SUM(age)` | 39 |
If every value in a group is NULL, SUM, AVG, MIN and MAX return NULL, while COUNT(age) returns 0. NULL grouping values are placed together in one group.
Empty input
| Query form | Input is empty | Result |
|---|---|---|
| No grouping, `COUNT` | yes | one tuple, value 0 |
| No grouping, `SUM` / `AVG` / `MAX` | yes | one tuple, value NULL |
| With grouping | yes | zero tuples |
Common errors
- Selecting a non-grouped attribute.
γ_{cid; COUNT(sid)}(Enrolled)cannot also returnsname. To get it, join the result back withStudent. - Filtering on an aggregate too early. A condition such as
COUNT(sid) >= 3is a selection afterγ, never inside theσbefore it. - Forgetting that groups disappear. Students with no enrollment have no tuple in
Enrolled, so they form no group. To report a count of 0 for them you need an outer join before grouping, andCOUNT(e.cid)rather thanCOUNT(*):
SELECT s.sid, COUNT(e.cid) AS num_courses
FROM Student s
LEFT OUTER JOIN Enrolled e ON s.sid = e.sid
GROUP BY s.sid;COUNT(*) here would return 1 for a student with no enrollment, because the outer join still produces one row for them.
Counting result tuples in GATE questions
- Identify the grouping attributes.
- Count the distinct combinations of those attributes that survive any earlier selection or join.
- Apply any outer selection on aggregates to remove groups.
- If there are no grouping attributes, the answer is 1.
Exercise
Relation R(A, B) contains (1, 5), (1, NULL), (2, NULL), (2, NULL), (3, 7). Evaluate γ_{A; COUNT(*) → c, COUNT(B) → d, SUM(B) → s}(R). How many tuples are returned, and what are they?
Show answer
Three tuples, one per distinct A. For A=1: c=2, d=1, s=5. For A=2: c=2, d=0, s=NULL (all B values are NULL). For A=3: c=1, d=1, s=7. The result is (1, 2, 1, 5), (2, 2, 0, NULL), (3, 1, 1, 7).
Exercise
Student has 5 tuples and Enrolled is empty. What does γ{COUNT(sid) → n}(Enrolled) return, and what does γ{cid; COUNT(sid) → n}(Enrolled) return?
Show answer
The first has no grouping attributes, so it returns exactly one tuple (0). The second has grouping attributes and an empty input, so there are no groups and it returns zero tuples.