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:
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.
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.
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.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 (orMINUS
in some database systems).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 aJOIN
condition. However, this is rarely used without a subsequentSELECT
operation, as it produces a lot of data.Rename (ρ): This operation renames the object. In SQL, this corresponds to the
AS
keyword for renaming columns or tables.
Derived operations include:
Intersection (∩): This operation returns all tuples that are present in both relations. This can be achieved in SQL through a combination of
UNION
andEXCEPT
, but some SQL dialects also provide aINTERSECT
operator.Division (/): The division operation is not directly available in SQL but can be simulated using basic operations.
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 likeINNER 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.