Study Vault Search notes, aliases and full text
Graph
ConceptDBISAI-written · not yet proofread

Relational Algebra

Relational algebra is the set of operations on tables (projection, selection, Cartesian product and derived operations such as join and group-by), each mapping one or two tables to a table, into which every SQL query is translated.

Know these firstRelational Model

Properties#

SQL ↔ operations#

Every query SELECT cols FROM T1, …, Tk WHERE φ equals Πcols(σφ(T1×⋯×Tk))\Pi_{\mathit{cols}}(\sigma_\varphi(T_1 \times \dots \times T_k)). Translating a query into a sequence of operations is query planning, executing it is query processing.

SQLOperation
SELECT [DISTINCT] colsprojection Πcols, distinct\Pi_{\mathit{cols},\,\mathit{distinct}}
WHERE φselection σφ\sigma_\varphi
FROM T1, T2Cartesian product T1×T2T_1 \times T_2
FROM T1 JOIN T2 ON T1.a = T2.bJoin T1⋈a,bT2T_1 \bowtie_{a,b} T_2 (VL05)
GROUP BY … HAVING …Grouping group-by (VL05)
ORDER BY, LIMITOrdering (by example only, VL05)

A sequence of operations can be written as a list of assignments, as one nested expression, or as a tree (leaves = input tables, inner nodes = operations, root = result); see Query Plan.

Derived operation: natural join#

T1⋈T2=Πshared cols once(σT1.c=T2.c ∀c∈C1∩C2(T1×T2))T_1 \bowtie T_2 = \Pi_{\text{shared cols once}}\big(\sigma_{T_1.c = T_2.c\ \forall c \in C_1 \cap C_2}(T_1 \times T_2)\big)

More generally, a Join is a selection on a Cartesian product that is computed without materialising the product (hash join, Merge Join).

Cost and size estimates#

Let TT have nn rows and kk columns, TiT_i have nin_i rows and kik_i columns, and k′=∣cols∣k' = |\mathit{cols}|:

Operationcostrowscols
Πcols, distinct(T)\Pi_{\mathit{cols},\,\mathit{distinct}}(T)∼n⋅k′\sim n \cdot k'∼n\sim nk′k'
σφ(T)\sigma_\varphi(T)∼n⋅k\sim n \cdot k∼n/2\sim n/2kk
T1×T2T_1 \times T_2∼n1⋅n2⋅(k1+k2)\sim n_1 \cdot n_2 \cdot (k_1 + k_2)n1⋅n2n_1 \cdot n_2k1+k2k_1 + k_2

The cost is proportional to the number of values read or written. Assumptions: tables stored in row order; φ\varphi evaluated in constant time and true for half of the rows (better: estimate selectivity on sample rows); the projection row estimate ignores duplicate removal. The product estimate is exact, since its result must be written anyway.

Equivalences used in query planning#

  • Push selections down: σφ1∧φ2(T1×T2)≡σφ1(T1)×σφ2(T2)\sigma_{\varphi_1 \wedge \varphi_2}(T_1 \times T_2) \equiv \sigma_{\varphi_1}(T_1) \times \sigma_{\varphi_2}(T_2) if each φi\varphi_i references only columns of TiT_i. Halving T1T_1 before the product halves the product’s cost n1n2(k1+k2)n_1 n_2 (k_1 + k_2).
  • Project early: dropping unneeded columns before a product reduces k1+k2k_1 + k_2 (WS 23/24 retake, Task 2.2).
  • Order of products: ×\times is associative and commutative up to a permutation of the columns; combine the small tables first.
  • Products → joins: σr[a]=r[b](T1×T2)≡T1⋈a,bT2\sigma_{r[a] = r[b]}(T_1 \times T_2) \equiv T_1 \bowtie_{a,b} T_2.

Example#

Sources#

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only