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
10
votes
4 answers

Clear explanation of the "theta join" in relational algebra?

I'm looking for a clear, basic explanation of the concept of theta join in relational algebra and perhaps an example (using SQL perhaps) to illustrate its usage. If I understand it correctly, the theta join is a natural join with a condition added…
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
10
votes
1 answer

How to understand `u=r÷s`, the division operator, in relational algebra?

let be a database having the following relational-schemes: R(A,B,D) and S(A,B) with the attributes of same name in the same domain and with the instances r and s respectively. An instance of r An instance of s What is the scheme and what are the…
Revolucion for Monica
  • 2,848
  • 8
  • 39
  • 78
10
votes
3 answers

Relational Algebra instead of SQL

I am studying relational algebra these days and I was wondering... Don't you thing it would be better if a compiler was existed which could compile relational algebra than compiling SQL? In which case a database programmer would be more…
Novemberland
  • 530
  • 3
  • 8
  • 25
9
votes
2 answers

Converting SQL to relational algebra

Does anyone know any good online resources which I can use to practise SQL to relational algebra conversion?
user559142
  • 12,279
  • 49
  • 116
  • 179
9
votes
3 answers

Relational Algebra - Cartesian Product vs Natural Join?

I am studying for exams and am failing to find a solid criteria by which I can determine if the Cartesian Product x is to be used or if Natural Join |X| is to be used. I had come up with a rough guide that: "If you need to project an attribute that…
Myles Gray
  • 8,711
  • 7
  • 48
  • 70
9
votes
3 answers

Relational Algebra equivalent of SQL "NOT IN"

Is there a relational algebra equivalent of the SQL expression NOT IN? For example if I have the relation: A1 | A2 ---------- x | y a | b y | x I want to remove all tuples in the relation for which A1 is in A2. In SQL I might…
jsj
  • 9,019
  • 17
  • 58
  • 103
8
votes
3 answers

Represent a subquery in relational algebra

How do I represent a subquery in relation algebra? Do I put the new select under the previous select condition? SELECT number FROM collection WHERE number = (SELECT anotherNumber FROM anotherStack);
AnEventHorizon
  • 205
  • 2
  • 3
  • 9
8
votes
3 answers

Lossless Join and Decomposition From Functional Dependencies

Suppose the relation R( K, L, M, N, P), and the functional dependencies that hold on R are: - L -> P - MP -> K - KM -> P - LM -> N Suppose we decompose it into 3 relations as follows: - R1(K, L, M) - R2(L, M, N) - R3(K, M, P) How can we…
7
votes
1 answer

SQL equivalent of relational algebra DIVISION

Here is an example T(A) = RENTED(A,C) / BOATS(C) select distinct R1.A from RENTED R1 where not exists (select * from SAILBOAT S where not exists (select * from RENTED R2 where…
Instinct
  • 2,201
  • 1
  • 31
  • 45
7
votes
3 answers

Relational algebra to count rows

I wasn't sure quite what to call this problem but it's not exactly counting rows. Let's say we have the relation: Competition(compId, sport, playerName, medal) And let's say the attribute medal can be either gold, silver, bronze, or null. So we…
Outback
  • 542
  • 2
  • 8
  • 20
7
votes
9 answers

Why are positional queries bad?

I'm reading CJ Date's SQL and Relational Theory: How to Write Accurate SQL Code, and he makes the case that positional queries are bad — for example, this INSERT: INSERT INTO t VALUES (1, 2, 3) Instead, you should use attribute-based queries like…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
6
votes
7 answers

Relations With No Attributes

Aheo asks if it is ok to have a table with just one column. How about one with no columns, or, given that this seems difficult to do in most modern "relational" DBMSes, a relation with no attributes?
cjs
  • 25,752
  • 9
  • 89
  • 101
6
votes
1 answer

Cartesian Product in Relational Algebra

I'm a total begginer in Relational Algebra and I don't manage to fully understand how cartesian product works. I want to know what happen in cartesian product when my two table have common attributes. I have no problem to understand when both table…
Peni
  • 626
  • 1
  • 7
  • 18
6
votes
1 answer

How to represent GROUP BY with HAVING COUNT(*)>1 in relational algebra?

For an exam, I am asked to get the list of clients having more than one rent, both as an SQL query and as an algebraic expression. For some reasons, the correction doesn't provide the algebraic version. So now I am left with: SELECT IdClient, Name,…
Pierre-Antoine Guillaume
  • 1,047
  • 1
  • 12
  • 28
6
votes
1 answer

What is relational data?

I'm not asking what a relational database is. I'm asking what relational data is. What makes data relational? What are some examples of relational data and of non-relational data that illustrate the difference? Edit: I now understand that there…
Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
1
2
3
38 39