Search code examples
sqldatabaserelational-algebra

Converting a certain SQL query into relational algebra


Doing an assignment for my database course and I want to double check my relational algebra.

The SQL:

SELECT dato, SUM(pris*antall) AS total
FROM produkt, ordre
WHERE ordre.varenr = produkt.varenr
GROUP BY dato
HAVING total >= 10000

The relational algebra:

σ total >= 10000 (
  ρ R(dato, total)(
    σ ordre.varenr = produkt.varenr (
      dato ℑ SUM(pris*antall (produkt x ordre)
    )
  )
)

Is this correct?


Solution

  • I don't know. And anybody else is not likely to know either.

    RA courses typically limit themselves to the selection, projection and join operators. Aggregations are not typically covered by an RA course. There even isn't any standard approach (that I know of) that the RA takes on aggregations.

    What is the operator that your course defines for doing aggregations on relations ? What type of value does that operator produce for its result ? A relation ? Something else ? If something else, how does your course explain doing relational restrictions on that result, given that these result values aren't relations, but restriction works only on relations ?

    Algebraically, this case starts with a natural join (produkt x ordre).

    [The result of] this natural join is subjected to an aggregation operation. Thus this natural join is to appear where you specify the relational input argument to your aggregation operator. The other needed specs for specifying the aggregation are the output attribute names (total), and the way to compute them (SUM(...)). Those might appear in subscript next to your aggregation operator symbol as "annotations", much like the attribute lists on projection and the restriction condition on restriction. But anything concerning this operator is course-specific, because there isn't any agreed-upon standard notation for aggregations, as far as I know.

    Then if your aggregation operator is defined to return a relation, you can specify your aggregation result as the input argument to a restriction with condition "total>=10000".