Relational Algebra

Relational algebra is a set of operations used to manipulate or retrieve data from relational databases. Its operations can be categorized into basic (or primitive) operations and derived (or additional) operations.

Basic operations include:

  1. Select (σ): This operation selects tuples (rows) that satisfy a given predicate. In SQL, this corresponds to the WHERE clause.

    For example, σage>20(Student) will select all tuples from the Student relation where the age is greater than 20.

  2. Project (π): This operation returns its argument relation with certain attributes left out. In SQL, this corresponds to specifying the column names in the SELECT clause.

    For example, πname, age(Student) will return a relation that includes only the 'name' and 'age' of all students.

  3. Union ( ∪ ): This operation returns all tuples that are in either or both of the two relations. Both relations must be union-compatible - i.e., the two relations involved must have the same set of attributes. In SQL, this corresponds to the UNION operator.

  4. Set Difference (-): This operation returns all tuples that are present in the first relation but not in the second relation. Both relations must be difference-compatible - i.e., the two relations involved must have the same set of attributes. In SQL, this corresponds to the EXCEPT operator (or MINUS in some database systems).

  5. Cartesian Product (X): This operation returns the cartesian product of the two relations. In SQL, this can be performed by listing two tables in the FROM clause without a JOIN condition. However, this is rarely used without a subsequent SELECT operation, as it produces a lot of data.

  6. Rename (ρ): This operation renames the object. In SQL, this corresponds to the AS keyword for renaming columns or tables.

Derived operations include:

  1. Intersection (∩): This operation returns all tuples that are present in both relations. This can be achieved in SQL through a combination of UNION and EXCEPT, but some SQL dialects also provide a INTERSECT operator.

  2. Division (/): The division operation is not directly available in SQL but can be simulated using basic operations.

  3. Join (⋈): This operation combines tuples from two relations if they satisfy a certain condition. In SQL, this corresponds to the JOIN operator (which has variations like INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN).

These operations form the foundation of data manipulation in relational databases, and SQL is a realization of these concepts in a form that can be used for practical databases.