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
6
votes
2 answers

Relational algebra - what is the proper way to represent a 'having' clause?

This is a small part of a homework question so I can understand the whole. SQL query to list car prices that occur more than once: select car_price from cars group by car_price having count (car_price) > 1; The general form of this in relational…
user251249
6
votes
2 answers

relational algebra expression for "is null"

What is the relational algebra expression of the query below? I couldn't find the expression for "Is Null". SELECT reader.name FROM reader LEFT JOIN book_borrow ON reader.cardid = book_borrow.cardid WHERE book_borrow.cardid Is Null;
Xtrageik
  • 71
  • 1
  • 1
  • 5
6
votes
5 answers

SQL Query theory question - single-statement vs multi-statement queries

When I write SQL queries, I find myself often thinking that "there's no way to do this with a single query". When that happens I often turn to stored procedures or multi-statement table-valued functions that use temp tables (of one sort or another)…
Michael Bray
  • 14,998
  • 7
  • 42
  • 68
6
votes
2 answers

What is OUTER UNION and why is it partially compatible

I am trying to understand how a OUTER UNION works, and why it is only partially compatible. I am aware this operation was created to take union of tuples from two relations if the relation are not type compatible (which I understand). Examples of…
orange
  • 5,297
  • 12
  • 50
  • 71
5
votes
4 answers

Can someone explain me how the cartesian product works in relational algebra

here it says Selection and cross product Cross product is the costliest operator to evaluate. If the input relations have N and M rows, the result will contain NM rows. Therefore it is very important to do our best to decrease the size of both…
jonathan
  • 291
  • 1
  • 5
  • 17
5
votes
7 answers

In SQL, how can I perform a "subtraction" operation?

Suppose I have two tables, which both have user ids. I want to perform an operation that would return all user IDS in table 1 that are not in table 2. I know there has to be some easy way to do this - can anyone offer some assistance?
Waffles
  • 121
  • 3
  • 7
5
votes
2 answers

Relational v Hierarchical data models

When the relational model was put forward by F.E. Codd, the established databases of the time used the hierarchical model. My understanding is that the relational model was felt to be a significant improvement on the hierarchical approach. My…
Benjohn
  • 13,228
  • 9
  • 65
  • 127
5
votes
3 answers

How to represent "not exists" in relational algebra?

How do I represent the SQL "not exists" clause in relational algebra?
sudh
  • 1,085
  • 4
  • 12
  • 13
5
votes
3 answers

subscript for a join (\bowtie) operation in LyX/LaTeX

I'm using LyX to write some Relational Algebra queries. I'm using the \bowtie symbol for the join operation but when I try to put a text in subscript directly under the symbol, I get the following…
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
5
votes
2 answers

Couchdb join two documents using key

I have two documents one with tree structure and the other one relation to the first doc. Im trying to join these two doc`s by fk and pk. I couldnt get the actual results and it displays all null values. First doc { "name": "one", "root": { …
vyeluri5
  • 487
  • 5
  • 8
  • 18
5
votes
2 answers

Seeking extended Divide operator explanation

I am reading about Codd’s Eight Original Operators in Inside Microsoft SQL Server 2008: T-SQL Querying by Itzik Ben-Gan, Lubor Kollar, Dejan Sarka, and Steve Kass and do not understand the Divide operator. Quotes defining the Divide operator: “A…
Jesslyn
  • 696
  • 10
  • 25
5
votes
2 answers

LEFT JOIN on a nullable column. What is the behaviour?

It is not clear to me what is the behaviour of a LEFT JOIN if you try to JOIN on a column that may be NULL. E.g. SELECT columns FROM EmployeePayment ep JOIN EmployeePaymentGroup g ON g.group_id = ep.group_id AND g.subsidiary_id =…
Jim
  • 18,826
  • 34
  • 135
  • 254
5
votes
4 answers

Are these two relations compatible for a union operation?

I'm not sure if the following two relations are compatible for a union: R: <- schema name B 1 2 2 3 3 And: Q: -< schema name A B 5 1 6 1 4 2 3 4 I want to do the union: Q U R. Can I? What's the result?
amorimluc
  • 1,661
  • 5
  • 22
  • 32
4
votes
3 answers

Are there any open-source query-language agnostic relational storage engines?

I've been studying proper relational algebra, from Christopher Date's book Database in Depth: Relational Theory for Practitioners. Throughout the book he uses the language he and Hugh Darwen came up with in order to convey the theory — Tutorial D. …
d11wtq
  • 34,788
  • 19
  • 120
  • 195
4
votes
2 answers

problem with select

I have a table with rows with two columns A 1 A 2 B 1 B 3 C 1 C 2 C 3 and I want to get from this only this ID(a,b or c) which has only 2 rows with value 1,2, so from this table I should get a, bacause b hasn't row with 2, and c has rows with 1…
user278618
  • 19,306
  • 42
  • 126
  • 196
1 2
3
38 39