Quick answer: Relational algebra is a procedural query language. Its operators take relations and return relations, and results never contain duplicate tuples. The six basic operators are selection σ (rows that satisfy a condition), projection π (chosen columns), cross product ×, union ∪, set difference − and rename ρ. Derived operators include intersection (R − (R − S)), joins (theta, equi, natural and outer joins) and division ÷, which returns values of R paired with every tuple of S. Set operations need union-compatible relations.
GATE gives small relations and asks for the output or the number of tuples of an expression, or which expression answers an English query. This topic is part of the Relational Algebra and SQL chapter.
Basic operators
Selection σ
returns the tuples of R that satisfy condition P. It keeps all columns.
Projection π
returns only the listed columns and removes duplicate rows.
Example: R(A, B, C) = {(8, 4, 5), (2, 4, 5), (7, 4, 6), (3, 5, 5)}.
- = {(4, 5), (4, 6), (5, 5)}. The duplicate (4, 5) appears once.
- = {(8, 4, 5), (7, 4, 6)}.
Cross product ×
R × S pairs every tuple of R with every tuple of S.
- Degree (arity) of R × S = degree(R) + degree(S).
- |R × S| = |R| × |S|. If either relation is empty, the product is empty.
Union, difference and intersection
For union-compatible R and S:
- R ∪ S: tuples in either relation.
- R − S: tuples in R but not in S.
- R ∩ S = R − (R − S): tuples in both.
Union compatibility needs the same arity and the same domain for corresponding attributes. Attribute names can differ. For example, (Sid, Sname) and (StudID, StudName) are compatible, but (Sid, Sname) and (Sid, Marks) are not, because Sname and Marks have different domains.
Example: R(A) = {2, 3} and S(B) = {2, 4}. Then R ∪ S = {2, 3, 4}, R − S = {3} and R ∩ S = {2}.
Rename ρ
- renames the relation to Temp.
- renames all attributes.
Rename is needed for self-joins, where a relation is compared with itself.
Joins
| Join | Definition |
|---|---|
| Theta (conditional) join | |
| Equi-join | A theta join whose condition uses only equality |
| Natural join | Equality on all common attribute names, keeping one copy of each |
| Left outer join (R ⟕ S) | Natural join plus unmatched R tuples padded with NULLs |
| Right outer join (R ⟖ S) | Natural join plus unmatched S tuples padded with NULLs |
| Full outer join (R ⟗ S) | Union of the left and right outer joins |
If R and S share no attribute names, their natural join is the same as their cross product.
Example: natural join of T1(A, B, C) and T2(B, C, D, E):
Division ÷
R(A, B) ÷ S(B) returns the A values in R that are paired with every B value in S. It answers "for all" queries.
Division using basic operators
Query: students enrolled in every course, with E = Enroll(sid, cid) and C = Course(cid).
Step 1: Find students who miss at least one course. Build every possible (student, course) pair and remove the real enrollments:
Step 2: Subtract them from all enrolled students:
Worked example
Enroll = {(s1, c1), (s1, c2), (s2, c1), (s3, c1), (s3, c2)} and Course = {c1, c2}.
Step 1: All pairs: {s1, s2, s3} × {c1, c2} gives 6 pairs.
Step 2: The only missing pair is (s2, c2), so the students missing a course are {s2}.
Step 3: Result = {s1, s2, s3} − {s2} = {s1, s3}.
"More than every" queries
Query: Emp(eid, sal, gen). Find women who earn more than every man.
Find the women who earn at most some man, then subtract them from all women:
For "more than some man", a single self-join with the condition T1.sal > T2.sal is enough.
Cardinality bounds
Here R has n tuples and S has m tuples, all distinct.
| Expression | Minimum | Maximum |
|---|---|---|
| R × S | n × m | n × m |
| Natural join R ⋈ S | 0 | n × m |
| Left outer join | n | n × m |
| Right outer join | m | n × m |
| Full outer join | max(n, m) | n × m |
| R ∪ S | max(n, m) | n + m |
| R ∩ S | 0 | min(n, m) |
| R − S | 0 | n |
| R ÷ S (m ≥ 1) | 0 | ⌊n / m⌋ |
Useful equivalences
- , and selections can be applied in any order.
- , so only the outermost projection matters.
- A selection that uses only R's attributes can be pushed inside a join: .
- ∪ and ∩ are commutative and associative; − is neither.
Summary table
| Operator | Remember |
|---|---|
| π | Removes duplicates |
| × | Arity adds, cardinality multiplies |
| ⋈ with no common attributes | Same as × |
| ∩ | R − (R − S) |
| ÷ | "For all"; at most ⌊n / m⌋ tuples |
| ρ | Needed for self-joins |
Exam tips
- For "every" or "all" in the question, think division, or "all candidates minus those that fail for some".
- For cardinality questions, check whether a key or foreign key limits the matches.
- Before using a natural join, list the common attribute names. An accidental shared name changes the result.
Common mistakes
- Keeping duplicates after projection. Relational algebra removes them.
- Using a natural join when names match by accident. It equates every common name.
- Writing division the wrong way round. The divisor's attributes must be a subset of the dividend's.
- Ignoring union compatibility before applying set operators.
Practice questions
Q1. R(A, B) has 5 tuples and S(B, C) has 4. B is a key of S, and every R.B value appears in S. How many tuples are in R ⋈ S?
Answer: Each R tuple matches exactly one S tuple, so 5.
Q2. R has 6 tuples and S has 3. What is the maximum number of tuples in R ÷ S?
Answer: ⌊6 / 3⌋ = 2.
Q3. Write R ∩ S using only basic operators.
Answer: R − (R − S).
FAQs
What are the basic operators of relational algebra?
Selection, projection, cross product, union, set difference and rename. Every other operator can be built from these.
What is the difference between a natural join and an equi-join?
An equi-join can equate any chosen attributes and keeps both columns. A natural join equates all attributes with the same name and keeps one copy of each.
When is the division operator used?
For queries with "all" or "every", such as students enrolled in every course or suppliers who supply every part.
Does relational algebra allow duplicate tuples?
No. Relational algebra works on sets, so every result has distinct tuples.
Why is the rename operator needed?
It lets you refer to the same relation twice under different names, which you need for self-joins.
Conclusion
Relational algebra questions get mechanical once you know what each operator does to rows, columns and duplicates. Practise division and "every" queries, and learn the cardinality table. Continue with tuple relational calculus and SQL queries, or go back to the Relational Algebra and SQL chapter.