Selection, Projection, and Join Order Optimization in DBMS: Worked Example
See how three equivalent relational-algebra plans return the same names while producing first-join intermediates of 14, 8, and 4 tuples.
KnowledgeGate Team
Exam prep & CS education

Two relational-algebra expressions can return exactly the same tuples while creating very different intermediate relations. Selection, projection, and join reordering work together because an optimizer tries to reduce rows and columns before expensive joins. In the Student, Enrollment, and Course query below, three equivalent plans all return {Asha, Dev}, while their first joins produce 14, 8, and 4 tuples. Use GATE CS Exam Preparation as the wider preparation hub.
1. What selection, projection, and join order each change
Selection sigma_condition(R) chooses rows. Projection pi_attributes(R) chooses columns. An equijoin R join_condition S combines matching tuples. Selection changes cardinality, projection changes degree and may remove duplicates under classical set semantics, and a join can change both.
The goal is to preserve the result while reducing intermediate data. Push a predicate towards the relation whose attributes it uses, keep only attributes needed later, and place a highly reducing join early when valid. Use Relational Algebra in DBMS: Complete Guide with Worked Queries for the full operator map, including set operations and division; Query Languages in DBMS: Relational Algebra, SQL and a Worked Query to express one result across algebra, SQL, and relational calculus; and Relational Algebra for GATE: Operators, Tuple-Counting and Query Equivalence Drills for general count bounds and equivalence drills. Logical optimization has a narrower target: push local selections and projections, then compare equivalent join orders by intermediate tuple counts.
These transformations assume classical relational algebra and inner equijoins. Predicate pushdown needs separate reasoning for outer joins, aggregates, and SQL bag semantics.
2. Build the running query and unoptimized algebra tree
The three relations are:
Student S(sid, name, dept) |
|---|
|
|
|
|
|
Course C(cid, title, dept) |
|---|
|
|
|
|
|
|
Enrollment E(sid, cid, grade) |
|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The query asks for CSE students who scored at least 8 in Database Systems:
SELECT S.name
FROM Student S
JOIN Enrollment E ON S.sid = E.sid
JOIN Course C ON E.cid = C.cid
WHERE S.dept = 'CSE'
AND E.grade >= 8
AND C.title = 'Database Systems';Start with the deliberately unoptimized expression:
pi_S.name(sigma_S.dept='CSE' AND E.grade>=8 AND C.title='Database Systems'((S join_S.sid=E.sid E) join_E.cid=C.cid C))
S join E returns all 14 enrollment matches. Joining those tuples with C still returns 14 because every cid matches. The combined selection keeps Asha's (1,DB101,9) and Dev's (4,DB101,10). Final projection gives {Asha, Dev}.
3. Push selections, then project required attributes
Split the predicate into relation-local filters:
sigma_dept='CSE'(S)gives 3 rows: Asha, Charu, and Dev. Selectivity here is3/5.sigma_grade>=8(E)gives 12 rows. It removes only(3,DB101,7)and(4,OS201,6), so the instance selectivity is12/14 = 6/7.sigma_title='Database Systems'(C)gives one row,(DB101,Database Systems,CSE), with selectivity1/6.
These fractions describe this particular instance, not every database. Now project after each filter has read its predicate attribute:
S1 = pi_sid,name(sigma_dept='CSE'(S)) = {(1,Asha),(3,Charu),(4,Dev)}
E1 = pi_sid,cid(sigma_grade>=8(E)) = {(1,DB101),(1,OS201),(1,AL202),(2,DB101),(2,EE110),(3,CN301),(3,OS201),(4,DB101),(4,CN301),(4,AL202),(5,DB101),(5,ME120)}
C1 = pi_cid(sigma_title='Database Systems'(C)) = {(DB101)}
Keep sid in S1 and E1, cid in E1 and C1, and name for the answer. Dropping a join key makes the later join impossible. Keeping dept, grade, or title after its filter only widens later tuples here.
4. Choose the join order by counting both alternatives

For the better order, E1 join C1 keeps {(1,DB101),(2,DB101),(4,DB101),(5,DB101)}, a 4-tuple intermediate. Joining it with S1 on sid keeps (1,Asha,DB101) and (4,Dev,DB101). Projection again gives {Asha, Dev}.
The other pushed-down order, S1 join E1, produces 8 tuples: 3 for Asha with DB101, OS201, AL202; 2 for Charu with CN301, OS201; and 3 for Dev with DB101, CN301, AL202. Joining these with C1 leaves Asha and Dev. It is equivalent, but its first intermediate is twice the 4-tuple result above.
Plan | First join output | Second join output before final projection | Final names |
|---|---|---|---|
Unoptimized | 14 | 14 |
|
Pushed, | 8 | 2 |
|
Pushed, | 4 | 2 |
|
The counts are exact for the data above and can vary across databases.

5. Equivalence is logical, while physical cost needs more
Inner joins are associative for compatible conditions, so parenthesization may change. A selection using only one input's attributes can move below an inner join. A projection can move down only if it retains every attribute needed above. These rules establish output equality; the counts 14, 8, and 4 reveal different intermediate volumes.
Tuple count is a useful exam heuristic, not a complete runtime model. Row width, indexes, I/O, memory, data distribution, sorting, and the join algorithm affect cost. Statistics estimate selectivity and cardinality, but not exactly. Access-path choices also depend on indexes and data layout. An index may help find DB101 or high grades, but it does not change {Asha, Dev}.
6. Common optimization traps and their corrections
Using projection as row filtering.
pi_grade>=8(E)is wrong because projection takes attributes. Usesigmafor tuple predicates.Projecting away a join key. Work backwards from the root. Retain output attributes, pending-predicate attributes, and every later join key.
Pushing a predicate into the wrong relation.
titlebelongs toCourse;gradebelongs toEnrollment. Label each condition by its source relation first.Assuming the smallest base relation must join first. Join output depends on matches. Here
E1 join C1is useful because it yields 4 enrollments.Applying inner-join rules to outer joins or SQL duplicates. Predicate movement can affect NULL-extended rows, and bag projection can retain duplicates. Use the semantics stated in the question.
7. How exams test selection, projection, and join ordering
A question may ask you to choose an equivalent expression, validate a pushdown, calculate cardinalities from given data, compare costed join trees, or identify an attribute that an early projection must retain.
Use this five-step routine:
Mark every predicate with its source relation.
Apply relation-local selections.
List output, pending-predicate, and join attributes before projecting.
Evaluate each join order from the leaves upwards.
Confirm that every candidate returns the same final tuples.
For this example, recover S1=3, E1=12, C1=1, E1 join C1=4, and {Asha, Dev}. For timed mixed practice, use the GATE Test Series.
8. The short version and the next step
Filter rows early. Trim columns only after preserving what later operators need. Compare join orders by the intermediate results they create. All three plans return {Asha, Dev}, but their first joins produce 14, 8, and 4 tuples.
Now change grade >= 8 to grade >= 9 without changing the data. E1 becomes 7 pairs. E1 join C1 becomes 3 pairs for sid values 1,4,5. After joining with CSE-only S1, the output remains {Asha, Dev} because Asha scored 9 and Dev scored 10 in DB101, while Farah's qualifying row is removed by the CSE filter.
Annotate a fresh algebra tree, redraw only the affected row counts, and check that every required join key remains.
Keep learning
Join Operations in DBMS: Inner, Left, Right and Full Outer Joins Worked Step by Step
Trace one pair of small tables through INNER, LEFT, RIGHT and FULL OUTER JOIN. See which unmatched rows survive, where NULL appears and how duplicate keys change counts.
Decomposition in DBMS: Lossless Join, Dependency Preservation and 4NF with Worked Examples
Learn the two decomposition checks through concrete joins, attribute closures, the 3NF and BCNF trade-off, and a worked 4NF example.
Inner and Outer Join MCQs: 12 Solved DBMS Questions with Explanations
Solve 12 practice MCQs on natural, inner, left and full outer joins. Each answer traces tuples, keys, degree or cardinality.
DBMS for GATE CS: Syllabus Areas, Question Weightage and Prep Order
Learn the DBMS syllabus in dependency order, see which areas recur most in PYQs, and practise the exact reasoning used in normalization, schedules, indexing and SQL questions.





