 Learn all about Computer Science
1. Introduction
Relational algebra is the mathematical model of the query languages used in relational database systems. Its operations take relations as input and produce relations as output.
In this tutorial, we’ll learn how to find the maximum value in relational algebra.
2. Relational Algebra
The basic operations in relational algebra are:
- Projection:
produces a new relation whose tuples have the coordinates that correspond to the columns of relation . It works as the SELECT operator in SQL
- Selection:
returns the subset of containing only the tuples for which the condition is true. We can think of it as the mathematical equivalent of the WHERE clause in SQL
- Rename: we can change the name of a relation, some or all of its attributes, or both the name and attributes simultaneously. We’ll denote this operation as
.
- Set operations: union, set difference (
), intersection, and Cartesian product ( )
This set of operations is usually extended with join operators ( ) and sometimes with aggregations ( ). The latter are equivalent to SQL aggregations, and in such extensions, finding the maximum is trivial, as MAX is a core operation of the algebra. Therefore, we’ll focus on implementing MAX using only the basic operations and joins.
3. Finding the Maximum
Let’s say we have a relation in which we record how many points contestants scored in a tournament:
id |
points |
A1 |
67 |
A2 |
78 |
A3 |
85 |
A4 |
85 |
The task is to find the contestant with the most points.
To solve it using relational algebra, we’ll first talk about defining a set’s maximum value.
3.1. Maximum vs Greatest Element
In mathematics, we differentiate between the maximal and greatest elements of a set.
The greatest element of a set is the one that is greater than all the other elements of that set:
![Rendered by QuickLaTeX.com \[x = \mathrm{greatest of } A \iff (\forall y \in A: y \neq x) x > y\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-0f9ccf8e7e1e1ba83558bb6271ec4001_l3.svg)
On the other hand, the maximal element is the one that is not less than any other element. Mathematically:
![Rendered by QuickLaTeX.com \[x = \max A \iff (\forall y \in A:) \neg (y > x)\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-433052e427188baf88e0ccc22f9fb55e_l3.svg)
We use the latter definition to implement MAX. We will do it in two steps:
- Find all the contestants with a lower score than at least one other contestant (this is the part
in the definition)
- Find the contestants that are not among these
The second step finds those with maximal points because if a contestant isn’t found in the first step, then no other contestant has more points, which is the maximum’s definition.
3.2. Implementation
To find the contestants with fewer points than at least one other contestant, we’ll first compute the Cartesian product of with a renamed version of itself. Then, we’ll filter the product relation.
Let’s do the renaming and compute the product:
![Rendered by QuickLaTeX.com \[\begin{aligned} Tournament_1 &\leftarrow \boldsymbol{\rho}_{Tournament_1(id_1, points_1)} Tournament \\ Tournament_2 &\leftarrow \boldsymbol{\rho}_{Tournament_2(id_2, points_2)} Tournament \\ Temp_1 &\leftarrow Tournament_1 \boldsymbol{\times} Tournament_2 \\ \end{aligned}\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-453c3335576f321fd5f4309465affb72_l3.svg)
This is the resulting relation:
id1 |
id2 |
points1 |
points2 |
A1 |
A1 |
67 |
67 |
A1 |
A2 |
67 |
78 |
A1 |
A3 |
67 |
85 |
A1 |
A4 |
67 |
85 |
A2 |
A1 |
78 |
67 |
A2 |
A2 |
78 |
78 |
A2 |
A3 |
78 |
85 |
A2 |
A4 |
78 |
85 |
A3 |
A1 |
85 |
67 |
A3 |
A2 |
85 |
78 |
A3 |
A3 |
85 |
85 |
A3 |
A4 |
85 |
85 |
A4 |
A1 |
85 |
67 |
A4 |
A2 |
85 |
78 |
A4 |
A3 |
85 |
85 |
A4 |
A4 |
85 |
85 |
Now, let’s keep only the tuples in which :
![Rendered by QuickLaTeX.com \[Temp_2 \leftarrow \boldsymbol{\sigma}_{points_1 < points_2} Temp_1\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-5f140e866044ea63a5c3922472862bc0_l3.svg)
This gives us:
id1 |
id2 |
points1 |
points2 |
A1 |
A2 |
67 |
78 |
A1 |
A3 |
67 |
85 |
A1 |
A4 |
67 |
85 |
A2 |
A3 |
78 |
85 |
A2 |
A4 |
78 |
85 |
The first column contains the contestants who “lost” to at least one other contestant, so they can’t have the maximum number of points. Let’s do the projection:
![Rendered by QuickLaTeX.com \[Fewer \leftarrow \boldsymbol{\pi}_{id_1} Temp_2\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-ee5fc8c73099949b7ef04373777e140e_l3.svg)
Now, we have those IDs in this unary relation:
The set difference of and contains the IDs of the contestants with the maximum number of points. We rename the attribute of to so that the attribute names match:
![Rendered by QuickLaTeX.com \[(\boldsymbol{\pi}_{id} Tournament) \setminus (\boldsymbol{\rho}_{id_1 \mapsto id}Fewer) = \{A1, A2, A3, A4\} \setminus \{A1, A2\} = \{A3, A4\}\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-838535a7a0dd7e0efd85104c47f9bb98_l3.svg)
This is the correct result, as the contestants A3 and A4 have the same number of points, more than A1 and A2. As we see, there can be more than one maximum.
The same logic applies to finding the minimal values. We only have to replace with .
3.3. Using Joins
Filtering the Cartesian product is essentially a join operation:
![Rendered by QuickLaTeX.com \[R \boldsymbol{\bowtie}_{\theta} S = \boldsymbol{\sigma}_{\theta} (R \boldsymbol{\times} S)\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-191bbca7d6c0f050cff3c36cbf2c9cc6_l3.svg)
So, we can streamline those two steps in the computation of maximum values:
![Rendered by QuickLaTeX.com \[\begin{aligned} Tournament_1 &\leftarrow \boldsymbol{\rho}_{Tournament_1(id_1, points_1)} Tournament \\ Tournament_2 &\leftarrow \boldsymbol{\rho}_{Tournament_2(id_2, points_2)} Tournament \\ Compared &\leftarrow Tournament_1 \boldsymbol{\bowtie}_{points_1 < points_2} Tournament_2 \\ Fewer &\leftarrow \boldsymbol{\pi}_{id_1} Compared \\ Result &\leftarrow (\boldsymbol{\pi}_{id} Tournament) \setminus (\boldsymbol{\rho}_{id_1 \mapsto id}Fewer) \end{aligned}\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-6908865fc0e2eefcf571f9c873ac0a55_l3.svg)
If another relation stores the contestants’ info, we can join the relations using the IDs to include those attributes in the result.
3.4. Showing the IDs and Maximal Values
If we want to see the maximal points in addition to the IDs, we should keep when applying the projection to :
![Rendered by QuickLaTeX.com \[\begin{aligned} Fewer &\leftarrow \boldsymbol{\pi}_{id_1, points_1} Compared \\ Result &\leftarrow Tournament \setminus (\boldsymbol{\rho}_{id_1 \mapsto id, points_1 \mapsto points} Fewer) \end{aligned}\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-e059d0d2325c51120ca69aad5d188662_l3.svg)
To get only the maximal number of points, we project :
![Rendered by QuickLaTeX.com \[\boldsymbol{\pi}_{points} Result\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-fe0e171d7f4523c43b5fae535dd8529d_l3.svg)
4. Finding the Maximum per Group
What if the tournament is not all vs. all, but contestants compete within groups?
Let’s say the relation is , where is the contestant’s ID:
group |
id |
points |
G1 |
A1 |
67 |
G1 |
A2 |
78 |
G1 |
A3 |
85 |
G2 |
B1 |
50 |
G2 |
B2 |
65 |
G2 |
B3 |
65 |
We start the same way as before, by renaming the relations:
![Rendered by QuickLaTeX.com \[\begin{aligned} Tournament_1 &\leftarrow \boldsymbol{\rho}_{Tournament_1(group_1, id_1, points_1)} Tournament\\ Tournament_2 &\leftarrow \boldsymbol{\rho}_{Tournament_2(group_2, id_2, points_2)} Tournament\\ \end{aligned}\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-8bc8a9f9edcce52ac06592011495b283_l3.svg)
However, when joining, we include the condition that in addition to . It means the comparisons will be performed only within groups:
![Rendered by QuickLaTeX.com \[Temp \leftarrow Tournament_1 \boldsymbol{\bowtie}_{group_1=group_2 \land points_1 < points_2} Tournament_2\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-e42bcb18a1192b9ada960d4feff23c5a_l3.svg)
This returns:
group1 |
id1 |
points1 |
group2 |
id2 |
points2 |
G1 |
A1 |
67 |
G1 |
A2 |
78 |
G1 |
A1 |
67 |
G1 |
A3 |
85 |
G1 |
A2 |
78 |
G1 |
A3 |
85 |
G2 |
B1 |
50 |
G2 |
B2 |
65 |
G2 |
B1 |
50 |
G2 |
B3 |
65 |
Now, we do the projection, keeping the attributes and in addition to :
![Rendered by QuickLaTeX.com \[Fewer \leftarrow \boldsymbol{\pi}_{group_1, id_1, points_1} Temp\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-b82810c82f8d1659f1534777a85ba782_l3.svg)
Since relations are sets, all the tuples are unique. So, the duplicates are filtered out automatically:
group1 |
id1 |
points1 |
G1 |
A1 |
67 |
G1 |
A2 |
78 |
G2 |
B1 |
50 |
We rename the attributes of to match those of and find the set difference of and the renamed :
![Rendered by QuickLaTeX.com \[\begin{aligned} Fewer_{renamed} &\leftarrow \boldsymbol{\rho}_{group_1 \mapsto group, id_1 \mapsto id, points_1 \mapsto points} Fewer \\ Result &\leftarrow Tournament \boldsymbol{\setminus} Fewer_{renamed} \end{aligned}\]](https://www.baeldung.com/wp-content/ql-cache/quicklatex.com-09ee0c5a9c02615181386c5c53132ebd_l3.svg)
This way, we get the contestants with maximum points per group:
group |
id |
points |
G1 |
A3 |
85 |
G2 |
B2 |
65 |
G2 |
B3 |
65 |
5. Conclusion
In this article, we discussed how to find the maximum in relational algebra. The idea is to find the set of values that are less than at least one other value in the relation. The maximal values are those that aren’t in that set. The post How to Find the Maximum Value in Relational Algebra first appeared on Baeldung on Computer Science.
Content mobilized by FeedBlitz RSS Services, the premium FeedBurner alternative. |