Relational Tuple Calculus for Databases and Query Languages

Gregory Cernera
5 min readJan 19, 2021
undraw.co

Introduction

This article will introduce you to the basic concepts of relational calculus. I will provide syntax, examples, and illustrations of several expressions to hopefully allow for easier comprehension. If you haven’t done so already, I would also suggest reading my article on relational algebra, which is another example of a relational query language.

What is relational tuple calculus?

Relational tuple calculus is a non-procedural query language. Put simply, this means that instead of describing how to retrieve data, you simply specify the data that you want to retrieve and the query language takes care of the rest. This is the foundation of SQL, the query language for designing and manipulating data in an RDBMS.

The relational algebra query language provides us with several operations that we can use to specify which tuples we want returned and which tuples we want excluded in the final relation.

Basic syntax for all expressions

The basic syntax is as follows…

{ t | R(t) }

t = the outputted tuples of the expression

R(t) = is the predicate that contains the conditions used to produce the resulting tuples

We will now take a look at some of the most common operations for relational calculus. I will provide you with the operation’s syntax, along with an example and some visuals to help you see why our expressions return a certain result.

For our first couple of examples, let’s use the following relation…

Selection

The selection operation allows you to retrieve rows from a relation. You could select every row from a relation, or select only rows that satisfy a certain condition.

Example 1.1:

Let’s say we want to “Find all Students”

{ s | s∈Students }

In literal terms, this expression states, “Find the set of tuples s such that s is an element of relation Students.

From this expression, we will get the following result…

Example 1.2:

What if we wanted to “Find all Students whose major is Math?” We can do that will the following expression…

{ s | s∈Students and (s.Major=’Math’) }

In literal terms, this expression states, “Find the set of tuples s such that s is an element of relation Students and s has a Major attribute equal to ‘Math.’”

Our result…

Example 1.3:

For our final selection, let’s “find all Students whose major is either Math or English”

{ s | s∈Students and (s.Major=’Math’ or s.Major=’English’) }

Literal terms: “Find the set of tuples s such that s is an element of relation Students and s has a Major attribute equal to ‘Math’ or ‘English’”

Our result…

Projection

The projection operation allows you to return specific columns from a relation, instead of returning every single one.

Example 2:

Query description: “Find the name and email of Students who major in Math”

{ s.Name, s.Email | s∈Students and (s.Major=’Math’) }

Our result…

Union

The Union operation allows you to union two sets of relational sets. In other words, union returns all instances between two sets, without including duplicate values.

Example 3:

First, let’s add a new column (Minor) to our Students relation…

Query description: “Union the set of Majors with the set of Minors”

{ x.Subject | ∃(s∈Students)(x.Subject=s.Major) OR ∃(s∈Students)(x.Subject=s.Minor)}

We decipher this expression by stating, “Return a set of tuples where the resulting values will be either in the set of Majors or the set of Minors.”

This will return the following result…

Intersection

The Intersection operation returns only the shared instances between two sets.

Example 4:

Query description: “Find the intersection between the set of Majors with the set of Minors”

{ x.Subject | ∃(s∈Students)(x.Subject=s.Major) AND ∃(s∈Students)(x.Subject=s.Minor)}

You may notice that this expression is very similar to the above Union example. That’s because it is; the only difference is that there is an AND instead of an OR. This will, therefore, return only shared instances between both sets.

We will get the following result…

Set Difference

The Set Difference operation returns tuples in set A that are NOT in set B

Example 5:

Query description: “Find the tuples in the Major set that are not in the Minor set

{ x.Subject | ∃(s∈Students)(x.Subject=s.Major) and NOT(∃(s∈Students)(x.Subject=s.Minor))}

Once again, this expression is very similar to the Intersection example above. The only difference is the introduction of “NOT” before the set of Minor tuples.

We will get the following result…

Natural Join

The Natural Join operation will compare two relations, match attributes that we want to compare, and return tuples whose values match each other. Only tuples that satisfy the expression will appear in the result. There will be no null values.

Example 6:

For this example, let’s change our schema a little bit by altering our Students relation and adding a new relation, Advisors…

Students and Advisors both share the attribute “AName,” so we will join these two relations on the AName attribute. We can write the expression like this…

{t.Name, t.Email, t.Major, t.AName, t.AdvEmail, t.Salary | ∃(s∈Students) ∃(a∈Advisors) (s.AName=a.AName and t.Name=s.Name and t.Email=s.Email and t.Major=s.Major and t.AName=a.AName and t.AdvEmail = a.AdvEmail and t.Salary=a.Salary) }

This expression looks like a lot. However, we are simply linking the two relations by writing s.AName=a.AName and then choosing which columns we want to display from each relation. This will give us the following result…

Conclusion

There you have it, some guidelines and examples of the most common relational tuple calculus operations! These rules make up the foundation for common database query languages such as SQL.

Thanks for reading! 👍

--

--