Relational algebra
The six basic operators of relational algebra (selection, projection, union, set difference, Cartesian product, rename), the derived operators (intersection, theta join, natural join), set semantics and union compatibility, evaluating expressions step by step on real relations, the bounds on the sizes of results, algebraic laws that always hold, and writing queries in algebra.
Why it exists
Relational algebra is the formal language in which queries are defined: a few operators that take relations and give relations. SQL is translated into it, and the optimiser rewrites it. GATE asks for the meaning of an expression on a given instance, for equivalent expressions, for the number of tuples a result can have, and for writing a query in algebra.
Prerequisites
You need relations as sets of tuples (relational model) and the set operations union, intersection, difference and product.
Precise definitions
All operators return relations, so they can be nested. Relations are sets: duplicates disappear automatically.
| operator | notation | result |
|---|---|---|
| selection | the tuples of that satisfy the condition (comparisons of attributes and constants with , combined with ) | |
| projection | the listed attributes of every tuple of ; duplicate tuples are removed | |
| union | tuples in or | |
| difference | tuples in and not in | |
| Cartesian product | every tuple of concatenated with every tuple of (attribute names must differ) | |
| rename | , | under a new name or with new attribute names |
| intersection | tuples in both | |
| theta join | pairs that satisfy | |
| natural join | pairs that agree on all common attributes, with the common attributes kept once |
Union compatibility. , and require the same number of attributes with compatible domains (in practice the same attribute list). The result takes the attribute names of the first operand.
The first six operators (σ, π, ∪, −, ×, ρ) are complete for the basic algebra: intersection and joins are derived from them. Selection, projection, union, difference and product are all needed: none can be expressed with the others (textbook statement, quoted; rename only gives names).
How it works
Selection and projection
Selection and projection: start
Computed, not typed| sid | sname | dept |
|---|---|---|
| 1 | Asha | CS |
| 2 | Bilal | EE |
| 3 | Chen | CS |
| 4 | Dara | ME |
Step 0
The relation Student has three attributes and four tuples. A relation is a SET of tuples: no order, no duplicates.
Selection keeps whole tuples and does not change the attributes; projection keeps all tuples (up to duplicates) but changes the attributes. has three tuples although Student has four, because the two CS students give the same value.
Selection can be cascaded and its conditions commute: . Projections cascade only downwards: .
Natural join
The natural join of Student, Enrolled and Course answers "who attends which course":
Natural joins: start
Computed, not typed| sid | sname | dept |
|---|---|---|
| 1 | Asha | CS |
| 2 | Bilal | EE |
| 3 | Chen | CS |
| 4 | Dara | ME |
| sid | cid | grade |
|---|---|---|
| 1 | C1 | A |
| 1 | C2 | B |
| 2 | C1 | B |
| 3 | C2 | A |
| 3 | C3 | A |
| cid | title | credits |
|---|---|---|
| C1 | DB | 4 |
| C2 | OS | 3 |
| C3 | CN | 3 |
| C4 | AI | 3 |
Step 0
Three relations. Student and Enrolled share the attribute sid, Enrolled and Course share cid.
. Dara (no enrolment) and the course C4 (no student) disappear: the inner join keeps only matching tuples. Natural join is commutative and associative, so the order of the three relations does not change the result (the order matters for cost, see the next page). If and have no attribute in common, ; if they have the same attributes, .
Set operations
Union, intersection and difference: start
Computed, not typed| sid |
|---|
| 1 |
| 3 |
| sid |
|---|
| 1 |
| 3 |
Step 0
Two union-compatible relations (same attributes, same domains): A = students of the CS department, B = students enrolled in C2.
With = sid of CS students and = sid of students enrolled in C2: lists both, the students in both groups, the CS students who are not enrolled in C2. Difference is the only way to express "not": students who took no course is .
Sizes of results
Let and .
| expression | number of tuples |
|---|---|
| between and | |
| between (if ) and (equal to if contains a key) | |
| between and | |
| between and | |
| between and | |
| exactly | |
| (common attributes) | between and |
| when the common attributes are a key of and a non-null foreign key of | exactly |
The bounds hold on 500 random instances, and the extreme join sizes are attained: no matching value gives ; all tuples of and with one common value give ( in the test); a foreign key into a key gives ().
Algebraic laws that hold (and some that do not)
Always true: , , , , , , and, if uses attributes of only, .
Not true in general: ; ; (the left side is contained in the right side). Example: , : but .
Writing queries
- Students who took C1 and C2: (an intersection; "or" would be a union or a disjunction in σ).
- Names of students who took no course: .
- Courses with more than 3 credits: .
- "Two different courses" needs rename: .
Worked examples
EX 1Evaluate on an instance
Easy has tuples (Asha, Chen); of that gives {Asha, Chen}. has tuples (CS, EE, ME).
EX 2Cardinalities
Easy, . : . : between and . : at most . : between and . A natural join: between and .
EX 3Maximum and minimum of a join
Mediumwith and with , join on . Minimum: (no common value). Maximum: (all tuples have the same ). If is the key of and a foreign key of (no NULLs): exactly . If is the key of only: at most .
EX 4Are these equivalent
Medium: yes (also ). : no (witness above). : yes. when mentions only attributes of : yes.
EX 5Typical questions
GATE level(a) "Which operators are needed to express 'students who did not take C1'?" difference (or an equivalent with ∖). (b) "How many tuples can have at most?" . (c) "Which of the following are union-compatible?" Same number of attributes with the same domains. (d) " is:" empty. (e) " and are equal when:" uses attributes in only. (f) "Is selection commutative with projection?" only when the selection's attributes are all kept by the projection.
Cheat box
Cheat box
- Basic operators: , , , , , . Derived: , .
- Relations are sets: projection removes duplicates. Union-compatibility needed for .
- ; ; ; ; join in ; foreign key into key: .
- , for on ; .
- "Not" needs difference; "for some" is a join/projection; "for all" is division.
Traps and GATE patterns
Connections
Builds on
Unlocks
Across subjects
No cross-subject links yet.
The operators are implemented by SQL (SQL basics); joins in depth, division and the optimisation of expression trees follow (advanced algebra); calculus is the declarative twin of algebra (tuple relational calculus).
PYQs
17 previous year questions from your book. I re-solved each one myself (10 by running or simulating, 7 by reasoning only); the book's key is shown next to my answer. All PYQs
Consider a relational database schema with two
relations R(P, Q) and S(X, Y)
Let E= {⟨u⟩|∃v∃w⟨u, v⟩∈R∧⟨v, w⟩∈S} be a
tuple relational calculus expression.
Which one of the following relational algebraic
expressions is equivalent to E ?- (a)ΠP(R⋈R.P=S.XS)
- (b)ΠP(S⋈S.X=R.QR)
- (c)ΠP(R⋈R.P=S.YS)
- (d)ΠP(S⋈S.Y=R.QR)
Consider the following two relations, R(A,B) and S(A,C): R A B 10 20 20 30 30 40 30 50 50 95 S A C 10 90 30 45 40 80 The total number of tuples obtained by evaluating the following expression σB<C(R⋈R.A=S.A S) is ___________.
The following relation records the age of 500 employees
of a company, where empNo (indicating the employee
number) is the key :
empAge(empNo,age)
Consider the following relational algebra expression
Πemp×No(empAge⋈(age>age1) ρempNo1,aye1(empAge))
What does the above expression generate?- (a)Employee numbers of all employees whose age is not the minimum.
- (b)Employee numbers of all employees whose age is the minimum.
- (c)Employee numbers of all employees whose age is more than the age of exactly one other employee.
- (d)Employee numbers of only those employees whose age is the maximum.
Consider the following relations P(X, Y, Z), Q(X, Y, T) and R(Y, V). Table : P X Y Z X1 Y1 Z1 X1 Y1 Z2 X2 Y2 Z2 X2 Y4 Z4 Table : Q X Y T X2 Y1 2 X1 Y2 5 X1 Y1 6 X3 Y3 1 Table : R Y V Y1 V1 Y3 V2 Y2 V3 Y2 V2 How many tuples will be returned by the following relational algebra query? πx(σ(P.Y = R.Y ∧ R.V = V2(P X R))) - πx(σ(Q.Y = R.Y ∧ Q.T > 2(Q X R)))
What is the optimized version of the relation algebra expression πA1(πA2(σF1(σF2(r)))), where A1, A2 are sets of attributes in r with A1 ⊂ A2 and F1, F2 are Boolean expressions based on the attributes in r?
- (a)πA1(σ(F1∧F2)(r))
- (b)πA1(σ(F1∨F2)(r))
- (c)πA2(σ(F1∧F2)(r))
- (d)πA2(σ(F1∨F2)(r))
Consider the following relation schema
Students(rollno:integer, sname: string)
Courses(courseno:integer,cname: string)
Registration (rollno: integer, courseno: integer, percent:
real)
Which of the following queries are equivalent to this query
in English?
"Find the distinct names of all students who score more
than 90% in the course numbered 107".
(I) SELECT DISTINCT S.sname
FROM Students as S, Registration as R
WHERE R.rollno = S. rollno AND R.courseno = 107 AND
R.percent> 90
(II) πsname(σcourseno= 107 Λ percent> 90 (Registration ⋈Students))
(III) {T|∃S ε Students, ∃R ε Registration (S.rollno =
R.rollno Λ R.courseno = 107 Λ R.percent> 90 Λ T.sname
= S.sname)}
(IV) {<SN>|∃SR∃RP(<SR,SN>∈Students∧<SR,107,RP>∈
Registration∧ RP>90)}- (a)I, II, III and IV
- (b)I, II and III only
- (c)I, II and IV only
- (d)II, III and IV only
Suppose R1(A, B) and R2(C, D) are two relation schemas. Let r1 and r2 be the corresponding relation instances. B is a foreign key that refers to C in R2. If data in r1 and r2 satisfy referential integrity constraints, which of the following is ALWAYS TRUE? DATABASE MANAGEMENT SYSTEM
- (a)ΠB(r1) – Πc(r2) = ∅
- (b)ΠC(r2) – ΠB(r1) = ∅
- (c)ΠB(r1) = Πc(r2)
- (d)ΠB(r1) – Πc(r2) ≠ ∅
The following functional dependencies hold for relations R(A, B, C) and S(B, D, E): B -> A , A -> C The relation R contains 200 tuples and the relation S contains 100 tuples. What is the maximum number of tuples possible in the natural join of R and S (R ⋈S) ?
- (a)100
- (b)200
- (c)300
- (d)2000
Let R and S be two relations with the following schema:
R(P, Q, R1, R2, R3)
S(P, Q, S1, S2)
where {P, Q} is the key for both schemas. Which of the
following queries are equivalent?
1. ΠP(R⋈S)
II. ΠP(R) ⋈ΠP(S)
III. ΠP(ΠP,Q(R) ∩ΠP,Q(S))
IV. ΠP(ΠP,Q(R) -(ΠP,Q(R) -ΠP,Q(S)))- (a)Only I and II
- (b)Only I and III
- (c)Only I, II and III
- (d)Only I, III and IV
Consider a selection of the form σA ≤ 100(r), where r is a relation with 1000 tuples. Assume that the attribute values for A among the tuples are uniformly distributed in the interval [0, 500]. Which one of the following options is the best estimate of the number of tuples returned by the given selection query?
- (a)50
- (b)100
- (c)150
- (d)200
Consider the following relation schemas : b -Schema = (b-name, b -city, assets) a-Schema = (a-num, b-name, bal) d-Schema = (c-name, a-number) Let branch, account and depositor be respectively instances of the above schemas. Assume that account and depositor relations are much bigger than the branch relation. Consider the following query: Πc-name (σb-city = "Agra" ∧ bal < 0 (branch ⋈ (account ⋈ depositor) Which one of the following queries is the most efficient version of the above query?
- (a). Πc-name (σbal <0(σb-city = "Agra" branch ⋈ account ) ⋈ depositor )
- (b). Πc-name (σb-city = "Agra" branch ⋈(σbal<0 account ⋈ depositor))
- (c). Πc-name ((σb-city = "Agra" branch ⋈σb-city = "Agra" ∧ bal < 0 account ) ⋈ depositor )
- (d). Πc-name (σb-city = "Agra" branch ⋈(σb-city = "Agra" ∧ bal < 0 account ⋈ depositor )) DATABASE MANAGEMENT SYSTEM
Consider the relations r1(P, Q, R) and r2(R, S, T) with primary keys P and R respectively. The relation r1 contains 2000 tuples and r2 contains 2500 tuples. The maximum size of the join r1⋈ r2 is :
- (a)2000
- (b)2500
- (c)4500
- (d)5000
Let r be a relation instance with schema R = (A, B, C, D). We define r1 = πA,B,C (r) and r2 = πA,D (r). Let s = r1 ⋈ r2, where ⋈ denotes natural join. Given that the decomposition of r into r1 and r2 is lossy, which one of the following is TRUE?
- (a)s ⊂ r
- (b)r ∪ s = r
- (c)r ⊂ s
- (d)r ⋈ s = s
The relation schema Person (pid, city) describes the city of residence for every person uniquely identified by pid. The following relational algebra operators are available: selection, projection, cross product, and rename. To find the list of cities where at least 3 persons reside, using the above operators, the minimum number of cross product operations that must be used is:
- (a)1
- (b)2
- (c)3
- (d)4 DATABASE MANAGEMENT SYSTEM
Consider the following relations A, B and C: A ID, Name, Age 12 Arun 60 15 Shreya 24 99 Rohit 11 B ID, Name, Age 15 Shreya 24 25 Hari 40 98 Rohit 20 99 Rohit 11 C ID, Phone, Area 10 2200 02 99 2100 01 How many tuples does the result of the following relational algebra expression contain? Assume that the schema of A ∪ B is the same as that of A. (A ∪ B) ⋈A.ID > 40 ∨ C.ID < 15 C
- (a)7
- (b)4
- (c)5
- (d)9
Consider the relation enrolled(student, course) in which (student, course) is the primary key, and the relation paid(student, amount), where student is the primary key. Assume no null values and no foreign keys or integrity constraints. Assume that amounts 6000, 7000, 8000, 9000 and 10000 were each paid by 20% of the students. Consider these query plans (Plan 1 on left, Plan 2 on right) to "list all courses taken by students who have paid more than x". A disk seek takes 4ms, disk data transfer bandwidth is 300 MB/s and checking a tuple to see if amount is greater than x takes 10 micro-seconds. Which of the following statements is correct?
- (a)Plan 1 and Plan 2 will not output identical row sets for all databases.
- (b)A course may be listed more than once in the output of Plan 1 for some databases
- (c)For x = 5000, Plan 1 executes faster than Plan 2 for all databases.
- (d)For x = 9000, Plan I executes slower than Plan 2 for all databases.
A table 'student' with schema (roll, name, hostel, marks), and another table 'hobby' with schema (roll, hobbyname) contains records as shown below: The following SQL query is executed on the above tables: select hostel from student natural join hobbywhere marks > = 75 and roll between 2000 and 3000; Relations S and H with the same schema as those of these two tables respectively contain the same information as tuples. A new relation S' is obtained by the following relational algebra operation: S' = ∏Hostel ((σ s.roll = H.roll(σmarks > 75 and roll > 2000 and roll < 3000 (S)) X (H)) The difference between the number of rows output by the SQL statement and the number of tuples in S' is .
- (a)6
- (b)4
- (c)2
- (d)0
Self-check
Question 1
has tuples and has . What are the smallest and largest sizes of and of ?
Show answerHide
: to . : to .
Question 2
Write in algebra: students enrolled in C1 but not in C2.
Show answerHide
.
Question 3
Is ?
Show answerHide
No: the left side is contained in the right side; e.g. , with the first attribute.
Question 4
What is the natural join of two relations with no common attribute?
Show answerHide
Their Cartesian product.
Revision card
One-screen revision
- , , , , , ; , derived; set semantics.
- Result sizes: product , union , difference , join .
- Laws: selection cascade, push-down through a join; projection does not distribute over difference or intersection.
- NULL never matches; "not" is difference.
References
- ·Silberschatz, Korth, Sudarshan, Database System Concepts (7th edition), Chapter 2, Sections 2.5 and 2.6 (relational algebra)
- ·Elmasri, Navathe, Fundamentals of Database Systems (7th edition), Chapter 8 (the relational algebra and relational calculus)
Verification
scripts/enum-checks/db_algebra.py: every operator implemented in scripts/db_lib.py (selection, projection, union, intersection, difference, product, natural join, theta join, left/right/full outer join, semijoin, grouping, division) equals the corresponding SQL query executed by SQLite on random relations (300 pairs including NULLs where SQL agrees, 200 more without NULLs); the laws on this page (selection cascade and commutation, projection cascade, union and join commutativity and associativity, intersection as double difference, selection push-down through a join, join as selection over a product, idempotence of the self natural join) hold on 300 random instances; the size bounds of each operator hold on 500 random instances, and the extreme join sizes (0, |R||S| and |R|) were constructed; the animations were recomputed with SQLite. The result of a query is a SET in the algebra (duplicates disappear); SQL's SELECT without DISTINCT would keep them. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and re-derived against the textbook definitions and the sample database. No defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)