Relational algebra: filter, choose and rename

Build a relational algebra expression by filtering tuples, choosing attributes and renaming a relation without confusing results with stored data. Relational algebra and set semantics · Databases.

A query can keep the right rows and still expose the wrong columns or name the wrong role. Relational algebra makes each transformation visible before SQL hides it in a compact statement.

Start with an expression, not a table edit

Relational algebra is a notation for describing a relation-shaped result. It reads an input relation and returns another relation; it does not edit the stored instance. The first scene uses Shipments(shipment_id, hub, state, eta_minutes) and asks for ready shipments with only their id and hub. The rows are qualitative examples: the point is where each operator acts.

Filter, shape and name a relation

Read the steps as text

  1. Shipments has concrete tuples; the ready predicate, requested attributes and result role are given as inputs while the operator sequence is withheld.
  2. The ready predicate is now the active question; both input tuples remain visible before any tuple is marked as surviving.
  3. The predicate keeps S-17 and removes S-18 from the intermediate relation; the input relation itself is unchanged.
  4. The selected tuple is visible with the requested output attributes shipment_id and hub; state is marked as an input column that will leave the result.
  5. The result now carries shipment_id and hub only; the selected relation remains a separate intermediate with its state attribute.
  6. The projected relation has the correct two attributes; a later expression needs to refer to this intermediate by the role name s.
  7. The projected relation is assigned the role name s for later reference; tuple values and the two-attribute shape stay unchanged.
  8. Selection filters tuples, projection chooses attributes and rename gives the intermediate a later reference; the base relation is not mutated.

Selection is written with sigma, σ, and keeps tuples whose predicate is satisfied. σ state = 'ready' (Shipments) filters tuples but does not remove held shipments from the stored relation. It is the algebraic counterpart of a row predicate. The predicate is evaluated against the input attributes named by the expression.

Projection is written with pi, π, and keeps only the named attributes: π shipment_id, hub (selected_shipments) → projected_shipments. In mathematical relational algebra the result is a set, so two tuples that become identical after projection appear once. SQL result queries commonly use bags and may preserve duplicate occurrences unless DISTINCT is requested. The algebraic expression and the SQL surface therefore need a stated semantic boundary. The same discipline helps when a later operator needs a column that an earlier projection removed from the intermediate result.

Rename is written with rho, ρ, and gives a relation or its attributes new names. ρ s(shipment_id, hub) (projected_shipments) creates the role name s for a later expression. Rename changes how the expression refers to values; it does not copy or rewrite the stored tuples.

Keep SQL behavior in its own box

The mathematical expression assumes relation sets and no inherent tuple order. SQL can preserve duplicate result rows, and neither algebra nor ordinary SQL promises display order without an explicit ordering request. A predicate involving SQL NULL is a separate three-valued-logic question: ordinary comparison may be UNKNOWN, and a SQL WHERE keeps only TRUE. Do not silently use SQL bag or NULL behavior as if it were a change to the relational operator definition.

The faded scene uses Inventory(item_id, warehouse, status, bin_code). It asks for available item ids and warehouses, then gives that two-attribute result a relation name for a later expression. The incoming case changes the warehouse and attribute names so the learner must apply the operator roles instead of repeating the shipment answer.

Composition is easier to audit when each intermediate relation has a name and a known shape. First write the input relation, then confirm that selection still exposes every attribute needed by the next predicate. After projection, only the listed attributes remain available to later expressions. Rename can make a role explicit when two expressions would otherwise use the same attribute name. It also makes later joins easier to read because each attribute has a visible owner. None of these steps means that the base relation was rewritten; they describe a value produced by the expression.

A useful distinction is between a relation expression and a SQL result presentation. Name the boundary whenever the learner moves from a pure relation to a database result. Algebra asks which tuples and attributes belong to the result. SQL may keep duplicate occurrences, let a NULL operand make a comparison evaluate to UNKNOWN (and WHERE retains only TRUE), or display rows in an implementation-chosen order. Those behaviors can be modeled around an expression, but they should be stated rather than smuggled into the symbols.

Apply the same operators to inventory

Read the steps as text

  1. Inventory contains concrete item tuples; available status, requested attributes item_id and warehouse, and the later role available_items are given.
  2. The available-status test is the active question; both inventory tuples remain visible before filtering.
  3. Only I-10 satisfies the available predicate; the inventory source is still separate from this intermediate result.
  4. The available tuple is visible, and the report requests only item_id and warehouse while status and bin_code remain source attributes.
  5. The output now carries item_id and warehouse only; status and bin_code remain outside the projected shape.
  6. The projected inventory relation is complete; a later expression needs a stable relation name available_items.
  7. The projected result receives the role name available_items without changing its tuple values.
  8. The fresh expression filters inventory, projects item_id and warehouse, then names the result available_items; SQL duplicate and NULL rules remain separate boundaries.

Recap

  • Selection filters tuples under a predicate and projection chooses attributes from the result.
  • Rename changes relation or attribute names for later references without changing tuple values.
  • Mathematical sets, SQL bags, SQL NULL predicates and display order are separate semantic choices.
Why the order of operators matters

π item_id (σ status = 'available' (Inventory)) first filters and then exposes item ids. Reversing the operations can lose the attribute needed by the predicate: once status is projected away, a later status selection has no named input. A query planner may rewrite equivalent expressions internally, but the expression still needs the attributes required by each predicate and the stated semantics must remain unchanged.

For a concrete identity connection, practise hash maps in Fundamentals: a map selects a value through a key, while algebra makes row and attribute transformations explicit. Next, the product and join lesson combines named relations.