Questions tagged [relational-algebra]

Relational Algebra is an offshoot of first-order logic and of the algebra of sets that deals with relations (sets of tuples). In Computer Science, Relational Algebra is commonly used when dealing with databases. Operators in Relational Algebra use relations as operands and produce a relation as a result.

Relational Algebra provides a formal system for working with relations. In the context of databases, it can be thought of as a more formal way of constructing queries on relations.

Relational Algebra supports the following operations:

  • Set operators: Union (∪), Difference (-), Intersection (∩) (relations must be union compatible i.e., the number of attributes and their domains must match).
  • Projection (π): This is a unary operation that lets you select a specific subset of attributes (columns) from a relation.
  • Selection (σ): This is a unary operation such that the expression σφ(R) lets you select a subset of relations from the relation R that satisfy a certain propsition φ. This proposition is expressed using logical operators where the atoms are of the form aθb where a and b refer to attributes and θ refers to a binary operation in the set {<, <=, =, >=, >}
  • Rename (ρ): This is a unary operation of the form ρa / b(R) where the result is identical to R except that the attribute a has been renamed to b. This operator can be used to rename attributes or the relation itself.
  • Natural join (⋈): Natural joins (⋈) is a binary operator that is written as (R⋈S) where R and S are relations. The result of the operation is the set of all combinations of tuples in R and S that have equal values on their common attribute names.
  • Theta Join (⋈aθb): A theta join is a conditional join where the result of the operation is the set of all combinations of tuples in R and S that satisfy the condition specified. Using the already-defined operators a theta join can be expressed as R⋈φS = σφ(R × S).
  • Division (÷): Division is a binary operation that is written as R ÷ S. Assuming R is defined as R(a, b) and S is defined as S(b), the result of R ÷ S is the relation T(a) where a tuple <a> is only in T if there are tuples in R of the form <a, b> such that an a in R is associated with every value of b in S.

When teaching databases, Relational Algebra is used as a formal foundation before the introduction of SQL (Structured Query Language). SQL is similar to Relational Algebra except that it supports a few extended operators. A few of the differences can be summed up as follows:

  • Relational Algebra is procedural whereas SQL is declarative
  • Relational Algebra deals with sets of tuples whereas SQL deals with bags.
  • Standard Relational Algebra does not support aggregate operators like summing.
  • Standard Relational Algebra does not support grouping.
  • Standard Relational Algebra does not support ordering or sorting.
  • Standard Relational Algebra does not support pattern-matching operators.

External links:

577 questions
131
votes
4 answers

What are projection and selection?

What is the difference between projection and selection? Is it: Projection --> for selecting the columns of table; and Selection ---> to select the rows of table? So are projection and selection vertical and horizontal slicing respectively?
prabhahar
111
votes
7 answers

Difference between a theta join, equijoin and natural join

I'm having trouble understanding relational algebra when it comes to theta joins, equijoins and natural joins. Could someone please help me better understand it? If I use the = sign on a theta join is it exactly the same as just using a natural…
maclunian
  • 7,893
  • 10
  • 37
  • 45
65
votes
8 answers

How can I find MAX with relational algebra?

Working with databases, how can I find MAX using relational algebra?
Ifiok Idiang
  • 705
  • 1
  • 6
  • 5
52
votes
5 answers

What is a Projection?

What is a Projection, in terms of database theory and NHibernate when using SetProjection()?
DaveDev
  • 41,155
  • 72
  • 223
  • 385
44
votes
10 answers

What is the difference between Select and Project Operations

I'm referring to the basic relational algebra operators here. As I see it, everything that can be done with project can be done with select. I don't know if there is a difference or a certain nuance that I've missed.
gizgok
  • 7,303
  • 21
  • 79
  • 124
24
votes
5 answers

Aggregate Relational Algebra (Maximum)

I am currently working on a homework assignment that requires a selection to occur that pulls out an element containing a specific attribute of maximum value compared to all other records. I've read a number of sources online that reference an…
XBigTK13X
  • 2,655
  • 8
  • 30
  • 39
22
votes
3 answers

Natural join if no common attributes

What will natural join return in relational algebra if tables don't have attributes with same names? Will it be null or the same as cross join (cross product) (Cartesian product)?
19
votes
3 answers

Algebra Relational sql GROUP BY SORT BY ORDER BY

I wanted to know what is the equivalent in GROUP BY, SORT BY and ORDER BY in algebra relational ?
Cyberflow
  • 1,255
  • 5
  • 14
  • 24
18
votes
3 answers

Selecting DISTINCT rows in relational algebra

There is a DISTINCT operator in SQL. However, I have an assignment in which I need to get some distinct values from a table, and I can only use relational algebra. Is there a way?
Karan
  • 11,509
  • 8
  • 34
  • 38
16
votes
9 answers

How to find all pizzerias that serve every pizza eaten by people over 30?

I'm following the Stanford Database course and there's a question where we have Find all pizzerias that serve every pizza eaten by people over 30 using Relational Algebra only. The problem consist of a small database with four…
Ivo Flipse
  • 10,222
  • 18
  • 50
  • 63
15
votes
8 answers

Relational Databases and Mathematics?

Can anyone suggest resources that take a mathematical approach to relational databases? Essentially relational algebra I'd guess. I have a mathematics background and now work a great deal with databases and would like to close the gap.
Steve Homer
  • 3,852
  • 2
  • 22
  • 41
14
votes
3 answers

Difference between Relational Algebra and Relational calculus

What is the exact difference between relational algebra and relational calculus? At most references, it will be Relational algebra is procedural and calculus is non-procedural. What do these terms mean? We can solve all problems using relational…
13
votes
2 answers

Unique constraint over multiple tables

Let's say we have these tables: CREATE TABLE A ( id SERIAL NOT NULL PRIMARY KEY ); CREATE TABLE B ( id SERIAL NOT NULL PRIMARY KEY ); CREATE TABLE Parent ( id SERIAL NOT NULL PRIMARY KEY, aId INTEGER NOT NULL REFERENCES A (id), …
troutwine
  • 3,721
  • 3
  • 28
  • 62
13
votes
6 answers

How to fetch distinct values with arel/relational algebra

I'm doing my best to bend my brain around arel and the relational algebra behind it, but how to represent a SELECT DISTINCT is consistently eluding my comprehension. Can anyone explain how to arel: SELECT DISTINCT title FROM posts; Many thanks!
jemmons
  • 18,605
  • 8
  • 55
  • 84
12
votes
4 answers

understanding the concept of Insertion Anomaly

I am learning insertion anomaly from www.sqa.org.uk/e-learning. Following data is written in it, An Insert Anomaly occurs when certain attributes cannot be inserted into the database without the presence of other attributes. For example this is the…
user379888
1
2 3
38 39