Joins, division and expression trees
Joins in depth (theta, equi, natural, left/right/full outer joins and semijoin) with their treatment of NULL, the division operator and its expression with the basic operators, grouping and aggregation, query evaluation as an expression tree, and the heuristic optimisation rules (push selections and projections down, combine a selection with a product into a join, order joins) with measured intermediate sizes.
Why it exists
The six basic operators are enough in theory but awkward in practice: a join is a selection over a product, "for all" needs a complicated expression, and a query written naively can be thousands of times slower than an equivalent one. This page adds the operators actually used and explains how an expression is evaluated and rewritten.
Prerequisites
You need the basic operators and their laws (relational algebra).
Precise definitions
- Theta join ; an equijoin is a theta join whose condition is a conjunction of equalities; the natural join is the equijoin on all common attributes with one copy of each kept.
- Left outer join R ⟕ S: the natural join together with the tuples of that have no match, padded with NULL for the attributes of . Right outer join R ⟖ S: the same for . Full outer join R ⟗ S: both.
- Semijoin : the tuples of that have a match in (only the attributes of ).
- Division. Let and . Then . It answers "for all" queries.
- Grouping and aggregation : group the tuples by the attributes in and compute aggregates (COUNT, SUM, AVG, MIN, MAX) per group.
How it works
Outer joins
Outer joins: start
Computed, not typed| eid | ename | did |
|---|---|---|
| 1 | Ravi | 10 |
| 2 | Sana | 20 |
| 3 | Tara | NULL |
| did | dname |
|---|---|
| 10 | Sales |
| 20 | HR |
| 30 | IT |
Step 0
Tara has no department (NULL) and the department IT (30) has no employee.
The inner join loses Tara (no department: her did is NULL, and NULL never matches) and the department IT (no employee). The outer joins bring back the unmatched side with NULLs. All three results were also computed by a real database engine (LEFT JOIN, RIGHT JOIN, FULL JOIN) and are identical. Relations of sizes and : inner left, right full, and each outer join is at least of the corresponding operand size when that operand is preserved.
Division
"Students enrolled in all the courses of Req" cannot be done with a join alone. Division is defined by an expression of the basic operators:
Read it as: take every candidate (first term); build all pairs of a candidate with a required value; subtract the pairs that really exist in to get the missing pairs; project to the candidates that miss something; subtract them from the candidates.
Division: start
Computed, not typed| sid | cid |
|---|---|
| 1 | C1 |
| 1 | C2 |
| 2 | C1 |
| 3 | C2 |
| 3 | C3 |
| cid |
|---|
| C1 |
| C2 |
Step 0
Which students are enrolled in ALL the courses of Req? Division answers 'for all' questions.
Only student 1 has both C1 and C2. The identity was checked on 300 random instances and against the double-NOT-EXISTS query a real engine runs. Properties: ; if is empty the result is every candidate (); the result has at most tuples.
Typical shapes. "Employees who work on all projects": . "Students who passed all courses taught by X": divide by the (projected) set of those courses. If the divisor is chosen by a condition, apply the selection to the divisor first.
Grouping and aggregates
gives one tuple per department with the number of students. Aggregates ignore NULLs (except COUNT(*)). Grouping is where the algebra goes beyond the basic operators: it cannot be expressed with . SQL's GROUP BY and HAVING are its direct counterparts (joins and aggregates).
Expression trees and equivalence rules
A query is evaluated as a tree whose leaves are relations and whose inner nodes are operators; each node produces an intermediate relation consumed by its parent. Two expressions are equivalent if they give the same result on every instance. Rules used by the optimiser:
- Selections cascade and commute: .
- A selection followed by a product is a join: .
- A selection that mentions attributes of one operand only can be pushed into it: .
- Joins are commutative and associative, so the order can be chosen.
- Projection can be pushed down if the attributes needed above (including join attributes) are kept.
- Selection distributes over union and difference: , . Projection distributes over union but not over difference or intersection.
Heuristic optimisation
Rules of thumb: (1) break conjunctive selections apart; (2) push selections as far toward the leaves as possible; (3) replace selection-over-product by joins; (4) do the most selective (smallest-result) joins first; (5) push projections down, keeping what is needed later. The goal is to keep the intermediate relations small. The query "attributes of the CS students' enrolments" can be written three ways, all with the same 4 result tuples:
Three equivalent expressions, three different amounts of work: start
Computed by a script, not typed| plan | intermediate results (tuples) | tuples produced in total | final result |
|---|---|---|---|
| Plan 1: σ dept='CS' ∧ sid=esid (Student × Enrolled) | |||
| Plan 2: push the selection down: σ dept='CS' (Student) ⋈ Enrolled | |||
| Plan 3: join first: σ dept='CS' (Student ⋈ Enrolled) |
Step 0
Student has 4 tuples and Enrolled 5. Pushing the selection towards the leaves and joining instead of taking a product keeps the intermediate relations small. The sizes are counted by running the expressions.
Plan 1 builds the whole product (20 tuples) before selecting. Plan 2 pushes the selection into Student first (2 tuples), then joins (4). Plan 3 joins first (5 tuples) and selects after (4). Work (sum of the intermediate sizes, a crude cost measure): 24, 6 and 9. On realistic tables the differences are huge, which is why the order matters even though the results are equal.
Worked examples
EX 1Which join keeps what
EasyEmp(1 Ravi 10, 2 Sana 20, 3 Tara NULL) and Dept(10 Sales, 20 HR, 30 IT): inner join tuples; left outer (Tara added); right outer (IT added); full outer .
EX 2A division query
EasyEnrolled(sid, cid) Req(cid) with Req = {C1, C2}: students who have both. For Enrolled = {(1, C1), (1, C2), (2, C1), (3, C2), (3, C3)}: only student 1.
EX 3Division with a condition on the divisor
MediumStudents who took every 3-credit course: divisor . Nobody is enrolled in C4 in the sample instance, so the result is empty. If C4 is dropped from Course, the result is the students with both C2 and C3: student 3.
EX 4Choose a better plan
MediumQuery: . As written, the product has tuples. Rewriting the selection and product as a join and pushing the selection on Student before the join gives and then intermediate tuples, a fraction of the work, for the same answer.
EX 5Typical questions
GATE level(a) "Division is used for:" queries with 'for all'. (b) "Which of the following is NOT equivalent:" is not equivalent. (c) "Number of tuples in at most:" the number of distinct -values of . (d) "The left outer join of and has at least how many tuples?" . (e) "Pushing a selection below a join is valid when:" the condition refers only to attributes of one operand. (f) "Full outer join contains:" the inner join plus the dangling tuples of both sides.
Cheat box
Cheat box
- Outer joins keep unmatched tuples with NULL padding: left keeps , right keeps , full keeps both. Inner join left, right full.
- Semijoin: tuples of with a match. Division: 'for all'.
- .
- Push selections down, replace σ over × by ⋈, join the small results first; projection pushes through ∪ only.
- Aggregation (γ) is beyond the six basic operators.
Traps and GATE patterns
Connections
Builds on
Across subjects
No cross-subject links yet.
SQL's JOIN, LEFT JOIN, GROUP BY and NOT EXISTS correspond to these operators (joins and aggregates); the same queries can be written in calculus (tuple relational calculus); the base operators are on the previous page.
PYQs
9 previous year questions from your book. I re-solved each one myself (7 by running or simulating, 2 by reasoning only); the book's key is shown next to my answer. All PYQs
Consider the following three relations in a relational database: Employee(eId, eName) Brand(bId, bName) Own(eId, bId) Which of the following relational algebra expressions returns the set of eIds who own all the brands?
- (a)π eId ( (π eId,bld (Own)) / π bId (Brand) )
- (b)π eId (Own) - π eId ( (π eId (Own) × π bId (Brand)) - π eId,bld Own ))
- (c)π eId( π eId,bld (Own) / π bId (Own)
- (d)π eId ( (π eId (Own) × π bId (Own)) / π bId (Brand) ) DATABASE MANAGEMENT SYSTEM
Consider the relations r(A, B) and s(B, C), where s,B is a primary key and r.B is a foreign key referencing s.B. Consider the query: DATABASE MANAGEMENT SYSTEM Q: r ⟕ (σ B<5 (s)) Let LOJ denote the natural left outer-join operation. Assume that r and s contain no null values. Which one of the following queries is NOT equivalent to Q?
- (a)σ B<5 (r ⋈ s)
- (b)σ B<5 (r LOJ s)
- (c)r LOJ (σ B<5 (s))
- (d)σ B<5 (r LOJ s)
Consider a database that has the relation schema CR(StudentName, CourseName). An instance of the schema CR is as given below: CR Table Student Name Course Name SA CA SA CB SA CC SB CB SB CC SC CA SC CB SC CC SD CA SD CB SD CC SD CD SE CD SE CA SE CB SF CA SF CB SF CC The following query is run on the database: T1 ← π CourseName (σ StudentName = 'SA' (CR)) T2 ← CR ÷ T1 The number of rows in T2 is ______.
Consider two relations R1(A, B) with the tuples (1, 5), (3, 7) and R2(A, C) = (1, 7), (4, 9). Assume that R(A, B, C) is the full natural outer join of R1 and R2. Consider the following tuples of the form (A, B, C): a = (1,5, null), b = (1, null, 7), c = (3, null, 9), d = (4, 7, null), e = (1, 5, 7), f = (3, 7, null), g = (4, null, 9). Which one of the following statements is correct?
- (a)R contains a,b,e,f,g but not c, d.
- (b)R contains all of a,b,c,d,e,f,g
- (c)R contains e,f,g but not a,b
- (d)R contains e but not f,g DATABASE MANAGEMENT SYSTEM
Consider the relational schema given below, where eld of the relation dependent is a foreign key referring to empld of the relation employee. Assume that every employee has at least one associated dependent in the dependent relation. employee(empld,empName,empAge) dependent(depld, eld,depName, depAge). Consider the following relational algebra query: ΠempId ( employee ) -ΠempId ( employee ⋈(empId=eID )∧( empAge ≤ depAge ) dependent ) The above query evaluates to the set of emplds of employees whose age is greater than that of
- (a)Some dependent
- (b)All dependents.
- (c)Some of his/her dependents
- (d)All of his/her dependents
Let R and S be relational schemes such that R = {a, b,
c} and S = {c}. Now consider the following queries on
the database:
1 πR-S(r) -πR-S(πR-S(r) × s-πR-S,S(r))
2. {t|t∈πR-S(r) ∧∀u∈s(∃v∈r(u= v[S] ∧t=
v[R-S]))}
3. {t|t∈πR-S(r) ∧∀v∈r(∃u∈s(u= v[S] ∧t=
v[R-S]))}
4. Select R.a, R.b
From R, S
Where R.c = S.c
Which of the above queries are equivalent?- (a)1 and 2
- (b)1 and 3
- (c)2 and 4
- (d)3 and 4
Information about a collection of students is given by
the relation
studInfo(studId, name, sex).
The relation enroll(studId, courseId) gives which
student has enrolled for (or taken) what course(s).
Assume that every course is taken by at least one male
and at least one female student.
What does the following relational algebra expression
represent?
πcourceId ((πstudId (σsex = 'female' ( studInfo )) ×
πcourseId ( enroll )) - enroll )- (a)Courses in which all the female students are enrolled
- (b)Courses in which a proper subset of female students are enrolled.
- (c)Courses in which only male students are enrolled.
- (d)None of the above.
A database table T1 has 2000 records and occupies 80 disk blocks. Another table T2 has 400 records and occupies 20 disk blocks. These two tables have to be joined as per a specified join condition that needs to be evaluated for every pair of records from these two tables. The memory buffer space available can hold exactly one block of records for T1 and one block of records for T2 simultaneously at any point in time. No index is available on either table. If Nested-loop join algorithm is employed to perform the join, with the most appropriate choice of table to be used in outer loop, the number of block accesses required for reading the data are .
- (a)800000
- (b)40080
- (c)32020
- (d)100
A database table T1 has 2000 records and occupies 80 disk blocks. Another table T2 has 400 records and occupies 20 disk blocks. These two tables have to be joined as per a specified join condition that needs to be evaluated for every pair of records from these two tables. The memory buffer space available can hold exactly one block of records for T1 and one block of records for T2 simultaneously at any point in time. No index is available on either table. If, instead of Nested-loop join, Block nested-loop join is used, again with the most appropriate choice of table in the outer loop, the reduction in number of block accesses required for reading the data will be
- (a)0
- (b)30400
- (c)38400
- (d)798400
Self-check
Question 1
How many tuples does the full outer join have if the inner join has tuples, tuples of and tuples of are unmatched?
Show answerHide
.
Question 2
Express with the basic operators.
Show answerHide
.
Question 3
Why is with a join condition better evaluated as a join?
Show answerHide
The product creates all pairs first; the join produces only the matching pairs.
Question 4
Is ?
Show answerHide
Yes: projection distributes over union (unlike difference and intersection).
Revision card
One-screen revision
- Outer joins keep unmatched tuples (NULL padding); semijoin keeps matching tuples of one side.
- Division = 'for all': .
- Optimise: split, push σ down, σ×→⋈, small joins first, push π with join attributes.
- Equivalent plans, different costs.
References
- ·Silberschatz, Korth, Sudarshan, Database System Concepts (7th edition), Sections 2.6 (additional relational-algebra operations) and 16.2 to 16.4 (equivalence rules and heuristic optimisation)
- ·Elmasri, Navathe, Fundamentals of Database Systems (7th edition), Sections 8.4 (additional operations) and 19.1 to 19.3 (query processing and heuristic optimisation)
Verification
scripts/enum-checks/db_algebra.py: left, right and full outer joins (NULL-free data for the full join, NULL-containing data for left and right), semijoin, grouping with COUNT, SUM and MAX, and division equal the SQL forms (LEFT/RIGHT/FULL JOIN, EXISTS, GROUP BY, double NOT EXISTS) executed by SQLite on random relations; the division identity R / S = pi(R) - pi((pi(R) x S) - R) holds on 300 random instances; the animation of the outer joins, the division and the plan sizes were recomputed with SQLite (Student x Enrolled has 20 tuples, 4 survive the selection; pushing the selection gives 2 + 4 tuples; joining first gives 5 + 4); the true equivalences (selection over union, difference and product, projection over union) hold on 300 random instances and witnesses show that projection does not distribute over difference or intersection. The optimisation rules are the standard heuristics, quoted. 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)