View on GitHub

Relational

Educational tool for relational algebra

Downloads | Screenshots | Query language | Grammar | Types | GUI | Optimizations |

Query language

This page explains the query language supported by relational.

Operators

It has 2 class of operators:

Binary operators

Operators without parameters work on two relations. The syntax for those operators is: relation OPERATOR relation.

SymbolNameExampleNotes
* product A * B
- difference A - B
union A ∪ B
intersection A ∩ B
÷ division A ÷ B
join A ⋈ B
left outer join A ⧑ B All outer joins use a python None value when they have no value to place.
right outer join A ⧒ B
full outer join A ⧓ B

Unary operators

Operators with parameters work on a single relation, and the result will depend on the passed parameters. The syntax for those is: OPERATOR parameters (relation)

Symbol Name Example Note
σ selection σ id==index or rank>3 (A) Expression must be written in python. The variables have the names of the fields in the relation.
If the expression contains parenthesis, it must be surrounded by another pair of parenthesis.
π projection π name,age (A)
ρ rename ρ old_name➡new_name,age➡old (A)

The language is formally defined here.

Precedence

Queries in parenthesis take precedence.
Unary operators take precedence over binary operators.
Everything is executed left to right.

You can use parenthesis to change priority: a ⋈ (q ∪ d)

Examples

These are some valid queries.

# Join people and skills
people ⋈ skills

# Select people within a certain age range
σ age > 25 and age < 50 (people)

# Selection with complicated expression requires an extra set of () around the expression
σ (name.upper().startswith('J') and age > 21) (people)

# Cartesian product of people with itself, including only name and id
ρ id➡i, name➡n (people) * π name, id (people)

For details on types: types in relational.