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

Updated 21 Nov 20265 min read
Selection, Projection, and Join Order Optimization in DBMS: Worked Example

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)

(1,Asha,CSE)

(2,Bharat,EE)

(3,Charu,CSE)

(4,Dev,CSE)

(5,Farah,ME)

Course C(cid, title, dept)

(DB101,Database Systems,CSE)

(OS201,Operating Systems,CSE)

(CN301,Computer Networks,CSE)

(AL202,Algorithms,CSE)

(EE110,Circuits,EE)

(ME120,Thermodynamics,ME)

Enrollment E(sid, cid, grade)

(1,DB101,9)

(1,OS201,8)

(1,AL202,10)

(2,DB101,8)

(2,EE110,9)

(3,DB101,7)

(3,CN301,9)

(3,OS201,8)

(4,DB101,10)

(4,OS201,6)

(4,CN301,8)

(4,AL202,9)

(5,DB101,9)

(5,ME120,8)

The query asks for CSE students who scored at least 8 in Database Systems:

sql
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 is 3/5.

  • sigma_grade>=8(E) gives 12 rows. It removes only (3,DB101,7) and (4,OS201,6), so the instance selectivity is 12/14 = 6/7.

  • sigma_title='Database Systems'(C) gives one row, (DB101,Database Systems,CSE), with selectivity 1/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

Side-by-side join-order diagram comparing 4-tuple E1 join C1 and 8-tuple S1 join E1 plans.

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 (S join E) join C

14

14

{Asha, Dev} after delayed selection

Pushed, S1 join E1 first

8

2

{Asha, Dev}

Pushed, E1 join C1 first

4

2

{Asha, Dev}

The counts are exact for the data above and can vary across databases.

Two relational-algebra trees for the same query: the unoptimized plan's first join makes 14 tuples, the pushed-down plan's makes 4.

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

  1. Using projection as row filtering. pi_grade>=8(E) is wrong because projection takes attributes. Use sigma for tuple predicates.

  2. Projecting away a join key. Work backwards from the root. Retain output attributes, pending-predicate attributes, and every later join key.

  3. Pushing a predicate into the wrong relation. title belongs to Course; grade belongs to Enrollment. Label each condition by its source relation first.

  4. Assuming the smallest base relation must join first. Join output depends on matches. Here E1 join C1 is useful because it yields 4 enrollments.

  5. 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:

  1. Mark every predicate with its source relation.

  2. Apply relation-local selections.

  3. List output, pending-predicate, and join attributes before projecting.

  4. Evaluate each join order from the leaves upwards.

  5. 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.