A relational algebra is a procedural query language consisting of a set of operations that take one or two relations as input and produce a new relation as their result.

There are six basic operators: (Selection), (Projection), (Union), (Set difference), (Cartesian product), and (Rename).

Note that there are equivalent queries; there may be multiple ways to write a query in relational algebra.

Selection

The unary selection operator selects tuples that satisfy a given predicate.

  • , where is a selection predicate and is a relation.
    • e.g.,
  • Comparisons uses in the selection predicate.
  • Predicates are combined using .
  • The select predicate may include comparisons between two different attributes.

Projection

The unary projection operator returns its argument relation with certain attributes removed.

  • , where s are attributes and is a relation.
    • e.g.,
  • Duplicate rows are removed from the result

Cartesian Product

The binary cartesian operator combines information from two relations.

It constructs a tuple of the result out of each possible pair of tuples.

  • , where and are relations.
    • e.g.,
  • To distinguish between attributes present in both relations, attach the name of the originating relation to the name of the attribute.
    • e.g.,

Join

Join operator is a composition of a cartesian product followed by a selection operation.

It returns only those tuples that satisfy some predicate(s), among all possible combinations of tuples from the two relations.

It does not belong to the six basic operations.

  • , where , are relations, and is a predicate on attributes on the schema .
    • e.g.,

Union

The binary union operator combines two relations.

It returns all tuples that are present in at least one of the relations.

Two relations must satisfy the condition.

  • , where and are relations.
    • e.g., Find all courses taught in fall 2017, or in spring 2018, or in both:

Intersection

The binary union operator combines two relations.

It returns all tuples that are present in both of the relations.

Two relations must satisfy the condition.

  • , where and are relations.

Set Difference

The binary union operator combines two relations.

It returns all tuples that are present in the preceding relation, but not in the succeeding relation.

Two relations must satisfy the condition.

  • , where and are relations.

Rename

The unary rename operator assigns a name to the results of relational algebra expressions.

It returns the result of the expression under the given name.

  • , where s are attributes, is a relational algebra expression, and is a name.

Assignment

Assignment operation assigns a relational algebra expression to a temporary relation variable.

It acts similar as the assignment operation in general-purpose programming languages.

  • , where is a name and is a relational algebra expression.

    • e.g., Find all instructors in any of the Physics or Music departments:

  • With the assignment operation, a query can be written as a sequential program consisting of a series of assignments, followed by an expression whose value is displayed as the result of the query.


Condition

  • Two relations must have the same arity.
  • The attributes of the two relations must be compatible.