Relational Algebra for LaTeX Cheat Sheet

In LaTeX

Relational algebra is a mathematical language used to describe and manipulate data stored in relational databases. It is a set of operations that can be used to query and manipulate data in a database. The operations in relational algebra are based on set theory and logic, and they provide a way to express complex queries in a concise and precise manner.

Relational algebra is an important tool for database designers and developers, as it provides a way to express complex queries in a concise and precise manner. It is also used in the development of query languages such as SQL, which is used to interact with relational databases. Understanding relational algebra is essential for anyone working with relational databases, as it provides a foundation for understanding how data is stored and manipulated in these systems.

This cheat sheet lists operators for relational algebra queries for LaTeX.

Basic Operators

OperatorDescription
$\sigma_{condition}(R)$Selects tuples from relation R that satisfy the given condition
$\pi_{attribute_1, attribute_2, …, attribute_n}(R)$Projects the specified attributes from relation R
$R \times S$Cartesian product of relations R and S
$R \cup S$Union of relations R and S
$R – S$Difference of relations R and S
$R \bowtie_{condition} S$Natural join of relations R and S that satisfy the given condition
$R \Join_{condition} S$Join of relations R and S that satisfy the given condition

Additional Operators

OperatorDescription
$\rho_{new_name}(R)$Renames relation R to new_name
$\delta(R)$Removes duplicate tuples from relation R
$\gamma_{attribute_1, attribute_2, …, attribute_n}(F)$Aggregates the specified attributes from relation F
$\leftarrow$Assignment operator
$\cup_{attribute}(R)$Returns the distinct values of the specified attribute from relation R
$\cap_{attribute}(R)$Returns the common values of the specified attribute from relation R
$\div$Division operator

Examples

Select

QueryResult
$\sigma_{age > 25}(Employees)$Selects all employees with age greater than 25
$\sigma_{name = ‘John’ \land age > 25}(Employees)$Selects all employees named John with age greater than 25

Project

QueryResult
$\pi_{name, age}(Employees)$Projects the name and age attributes from Employees
$\pi_{name}(Employees \bowtie_{department_id = 1} Departments)$Projects the name attribute from the join of Employees and Departments on department_id

Cartesian Product

QueryResult
$Employees \times Departments$Returns the Cartesian product of Employees and Departments

Union

QueryResult
$R \cup S$Returns the union of relations R and S

Difference

QueryResult
$R – S$Returns the difference of relations R and S

Natural Join

QueryResult
$Employees \bowtie_{department_id = id} Departments$Returns the natural join of Employees and Departments on department_id and id

Join

QueryResult
$Employees \Join_{department_id = id} Departments$Returns the join of Employees and Departments on department_id and id

Rename

QueryResult
$\rho_{NewEmployees}(Employees)$Renames Employees to NewEmployees

Duplicate Removal

QueryResult
$\delta(Employees)$Removes duplicate tuples from Employees

Aggregation

QueryResult
$\gamma_{department_id, COUNT(*)}(Employees)$Counts the number of employees in each department
$\gamma_{department_id, AVG(salary)}(Employees)$Calculates the average salary for each department

Assignment

QueryResult
$NewEmployees \leftarrow \sigma_{age > 25}(Employees)$Assigns the result of the select query to NewEmployees

Distinct Values

QueryResult
$\cup_{department_id}(Employees)$Returns the distinct department_id values from Employees

Common Values

QueryResult
$\cap_{department_id}(Employees)$Returns the common department_id values from Employees

Division

QueryResult
$R \div S$Returns the division of relations R and S

Reference

Relational Algebra – Wikipedia