Database Systems · Relational Calculus
Procedural vs Declarative Languages
Overview
Relational algebra and relational calculus are both query languages for the relational model, and both are useful in practice for implementing queries or building a practical query language on top of them.
They differ fundamentally in style: relational algebra is procedural, while relational calculus is declarative. Relational algebra also serves as the reference standard — the "measuring stick" — for relational completeness.
Key concepts
- Procedural language: you specify what data you want from the relations (tables) and how to get it — you write the procedure too, e.g. "join these two tables, then from those two tables take this".
You are not only saying what you want, you are also saying how you want it. - Declarative language: you state only what you want; you do not get into the details of how it is obtained. The "how" is left abstract.
- Relational algebra is procedural.
- Relational calculus is declarative (non-procedural).
- Equivalent expressive power: relational algebra, tuple relational calculus (TRC) and domain relational calculus (DRC) are all equivalent in power — any query you can write in relational algebra can also be written in TRC and in DRC, and vice versa.
- Relationally complete: a language is relationally complete if it can express everything that relational algebra can express.
- Measuring stick: relational algebra was developed earlier than relational calculus, so it is kept as the reference against which all other query languages are measured.
How it works
- Procedural style in relational algebra: you explicitly mention what data you want from the relations/tables, and you also mention how to get it (e.g. "join these two tables and from those two tables obtain the result").
If an expression is a complex expression made up of many sub-expressions, you also specify the order in which the operations must be applied — for a query with many operations, the entire sequence is spelled out.
Hence the entire procedure is given, which is what makes it a procedural language. - Declarative style in relational calculus: you state only what information you want from the database/relation ("just give me this, that's it").
You do not say "first do this, then on that output do this" — there is no step-by-step procedure at all, and the method of evaluation is left abstract. That is why relational calculus is simply a declarative language. - Relational completeness as a commercial requirement: if you invent a new practical language (e.g. SQL) and try to sell it as a vendor, the specifications should state that your language is relationally complete — meaning whatever queries can be expressed in relational algebra can also be expressed in your language.
Every language is supposed to be relationally complete; otherwise it will not be able to express some queries, i.e. it will not be able to extract some data from the database.
That is why all languages try to be relationally complete. - Consequence for TRC and DRC: since relational algebra, TRC and DRC are equivalent in power, and relational algebra is relationally complete by definition, both TRC and DRC are relationally complete.
So all three are relationally complete.
Worked examples
Selling a newly invented practical language (e.g. SQL)
- Step 1: As a vendor, you invent a new practical query language (SQL is the example given).
- Step 2: You must decide what to claim in the product specification.
- Step 3: The claim to make is: "this language is relationally complete".
- Step 4: Meaning of the claim: every query expressible in relational algebra is also expressible in your language.
- Step 5: Test used: relational algebra acts as the measuring stick; if your language matches (or exceeds) relational algebra's expressive power, it passes.
Is TRC relationally complete?
- Step 1: Relational algebra is procedural and is the reference for relational completeness.
- Step 2: TRC is equivalent in power to relational algebra (any RA query can be written in TRC).
- Step 3: Therefore TRC is relationally complete. The same reasoning applies to DRC.
- Conclusion: relational algebra, TRC and DRC are all relationally complete.
Comparison
| Aspect | Relational Algebra | Relational Calculus (TRC / DRC) |
|---|---|---|
| Style | Procedural | Declarative (non-procedural) |
| What is specified | What data is wanted and how to get it | Only what data is wanted |
| Procedure / steps | Entire procedure given; order of operations among many operations is specified | No procedure; "how" is left abstract |
| Developed | Earlier (used as reference) | Later |
| Expressive power | Equivalent to TRC and DRC | Equivalent to relational algebra and to each other |
| Relational completeness | Relationally complete (the standard) | Both TRC and DRC are relationally complete |
Revision summary
- Relational algebra = procedural: states what + how, including the order of operations in a complex expression.
- Relational calculus = declarative: states only what; never says "do this, then on the output do this".
- Relational algebra, TRC and DRC are equivalent in power — any query in one can be written in the others.
- Relationally complete = a language can express everything that relational algebra can express.
- Relational algebra is the measuring stick for all other query languages because it was developed first.
- A new practical language (e.g. SQL) must be declared relationally complete in its specifications; otherwise some queries/data cannot be expressed or extracted.
- All three — relational algebra, TRC, DRC — are relationally complete.
Previous-year exam insights
- A query language is relationally complete if it can express every query expressible in relational algebra (Codd's criterion); relational algebra, safe TRC and safe DRC are all relationally complete and equally expressive.
Asked inGATE 2002 - Unrestricted (unsafe) relational calculus is strictly more expressive than relational algebra; the equivalence with relational algebra holds only for the safe subsets of TRC and DRC.
Asked inGATE 2002 - Safety restriction: a safe calculus expression must have a finite result — every variable in the expression must be constrained to range over the values appearing in the database (or over a finite domain), which rules out expressions such as {t | ¬(t ∈ R)} that would produce infinite relations.
Asked inGATE 2002 - Relational algebra is procedural (specifies an order of operations), whereas relational calculus is declarative (specifies only the required result, with no evaluation order); both are formal query languages of the relational model.
Asked inGATE 2002 - SQL is based on relational algebra/calculus and is relationally complete (its core, without extensions such as aggregates or recursion, can express every relational-algebra query).
- TRC uses tuple variables bound by ∃/∀ with membership conditions (t ∈ R), while DRC uses domain variables ranging over attribute values; the two are equivalent in expressive power.