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

How to use a set algebra operation in SQL

I need to find the distinct drinkers who like either BEER or RED WINE. However, the query must be implemented with a set algebra operation. Here is what I have so far: SELECT DRINKER FROM LIKES WHERE DRINK = 'BEER' OR 'RED WINE'; This is not…
bean
  • 329
  • 1
  • 5
  • 13
3
votes
1 answer

SQL -> Relational Algebra

Suppose I have the following relations: Branch (branchNo(PK), street, city, postcode) Staff (staffNo(PK), fName, lName, sex, branchNo(FK)) Not that it matters for this question, but PK = primary key & FK = foreign key How would I write the…
user559142
  • 12,279
  • 49
  • 116
  • 179
3
votes
2 answers

How to fetch distinct values with arel/relational algebra and has_many :through

When I try to display all movies that a person is in, and they have more than 1 role (director,writer,actor) in a movie, I get multiple lines for that movie. If I add .select('DISTINCT id') or movies.* to try and eliminate the dups I get the…
shirha
  • 31
  • 1
  • 4
3
votes
1 answer

What is the difference between theta join and inner join?

theta join and inner join look identical to me: they are Cartesian product followed by an arbitrary selection. Or am I missing their differences? Thanks. The above wikipedia link says a theta-join takes a comparison & two attributes. But that is…
Tim
  • 1
  • 141
  • 372
  • 590
3
votes
2 answers

Is it possible to decorrelate all correlated SQL subqueries?

(Note: Not a duplicate of this question, since that question had a specific query in mind. This is more from a general, theory perspective.) I am learning about databases in college, and I have learned that SQL databases, in order to execute a…
TheHans255
  • 2,059
  • 1
  • 19
  • 36
3
votes
1 answer

What would this projection return?

I'm new to sql, if I run this projection on a table, would it return nothing or an error of this projection is not allowed (syntax)? I put all infos of the table and the projection in this picture below to make it easier to read :)
Alicia Doe
  • 33
  • 5
3
votes
1 answer

Updating in relational algebra

Consider a relation Human(name, age, height, weight). To update an attribute, e.g. age for the whole relation, I can write Human <- π name,age*2,height,weight (Human). But what if I want to update just some specific tuple? From SQL: UPDATE Human SET…
EasternHawk
  • 31
  • 1
  • 4
3
votes
2 answers

Why is it harder to query for entities with more than every vs less than some?

This is the database, bold primary key. works (person_name, company_name, salary) Suppose we have this problem: Find the names of all employees who earn more than every employee of Small Bank Corporation. Proposed solution: We cannot easily…
user3656099
  • 155
  • 1
  • 11
3
votes
2 answers

How to represent relational division(basic algebra expression) in terms of SQL

Query: Find names of sailors who have reserved all boats This can be represented in relation algebra as: 1. πsname ( ((σsid,bid Reserves) / (σbid Boats)) ⋈ Sailors) As per Relational algebra, Division can also be represented using basic algebra…
Ankita Rane
  • 171
  • 1
  • 1
  • 12
3
votes
1 answer

Rewrite multiple right joins to left joins

I read that SELECT * FROM table_0 RIGHT JOIN table_1 ON table_0.col_0 = table_1.col_0; is the same as: SELECT * FROM table_1 LEFT JOIN table_0 ON table_0.col_0 = table_1.col_0; How do I rewrite a longer query, let's say: SELECT * FROM …
Gregzor
  • 302
  • 1
  • 3
  • 8
3
votes
2 answers

Stuck with Logical SQL Query optimizations in Relational Algebra (OR in WHERE)

I am stuck with optimizing this SQL-Query in Relational Algebra: SELECT * FROM R1, R2, R3, R4 WHERE (R1.A = '1' OR (R2.B = '2' AND R3.C = R4.C)) AND R4.D = '4' I translated it to the following Relational Algebra statement: σ{R1.A='1' ∨ (R2.B='2'…
Christoph S
  • 697
  • 1
  • 6
  • 29
3
votes
1 answer

Find the sids of the suppliers who supply every part

Suppliers(sid, sname, address) Parts(pid, pname, colour) Catalog(sid, pid, cost) The answer to 'find all the suppliers who supply every part' is: SELECT C.sid FROM Catalog C WHERE NOT EXISTS ( SELECT P.pid FROM Parts P …
tryingtolearn
  • 2,528
  • 7
  • 26
  • 45
3
votes
1 answer

DUM and DEE differences in SQL

I would like to know the difference between DUM and DEE. Can anybody spot the difference with real world example?
Amir
  • 16,067
  • 10
  • 80
  • 119
3
votes
2 answers

Relational Algebra - Finding EXACTLY two values

Below is a Table and simple SQL to find the cities that occur in exactly two states Name State ---- ----- Paris MO Paris TX Paris VA Austin MA Austin TX Burling VT Result: Only Austin will qualify as it occurs in exactly two…
3
votes
1 answer

How to convert SQL to Relational Algebra in case of SQL Joins?

I am working on SQL and Relational Algebra these days. And I am stuck on the below questions. I am able to make a SQL for the below questions but somehow my Relational Algebra that I have made doesn't looks right. Below are my tables- Employee…
user2467545