Sub topic 3
Selection and projection
This chapter assumes you know basic set theory (union, subset, cardinality) and can read a table as rows and columns. Selection and projection are the two unary operators of relational algebra: each takes one relation and returns one relation. Almost every later operator in this tutorial is combined with them.
Running example
Every example uses this instance of Student(sid, sname, age).
| sid | sname | age |
|---|---|---|
| 1 | Asha | 19 |
| 2 | Ravi | 21 |
| 3 | Meera | 22 |
| 4 | Ravi | 21 |
Selection: choosing rows
Selection, written σ, keeps the tuples that satisfy a condition and drops the rest. The schema does not change. Only the number of tuples can change.
σ_age > 20 (Student)The result is the tuples with sid 2, 3 and 4. A condition may compare an attribute with a constant or with another attribute. It may also combine comparisons with ∧ (and), ∨ (or) and ¬ (not).
σ_(age > 20 ∧ sname = 'Ravi') (Student)This returns the tuples with sid 2 and 4.
Two facts matter for counting questions:
- |σ_c(R)| ≤ |R|. The result can be empty but never larger than the input.
- Selection can be cascaded. σc1(σc2(R)) = σ_(c1 ∧ c2)(R), and the order of the two conditions does not matter.
Projection: choosing columns
Projection, written π, keeps the listed attributes and drops the others. Because a relation is a set, projection also removes duplicate tuples.
π_sname (Student)The input has four tuples, but the result is {Asha, Ravi, Meera}, which has three. Ravi appears twice in the input and once in the output.
The size of a projection depends on the attributes you keep:
- |π_A(R)| ≤ |R|.
- If A contains a superkey of R, no two tuples agree on A, so |π_A(R)| = |R|.
- If A has only one distinct value across the relation, the result has exactly one tuple (when R is nonempty).
- The order of attributes in the list does not change the information in the result.
Composing the two operators
The query "names of students older than 20" needs both operators. Selection comes first because the condition uses age, and age would be gone after the projection.
π_sname (σ_age > 20 (Student))The selection leaves sid 2, 3 and 4. The projection keeps sname and removes the duplicate Ravi, so the answer is {Ravi, Meera}.
Swapping the order here is an error:
σ_age > 20 (π_sname (Student))This expression is invalid because age is no longer in the schema after the projection. The general rule is that πA(σc(R)) = σc(πA(R)) only when every attribute used in c belongs to A.
Projection can also be cascaded, but only toward the smaller list. πA(πB(R)) = π_A(R) when A ⊆ B. If A is not a subset of B, the outer projection refers to missing attributes, so the expression is invalid.
Translation to tuple calculus and SQL
The same query in the three formalisms:
Relational algebra: π_sname (σ_age > 20 (Student))
Tuple calculus: { t | ∃s ∈ Student (s.age > 20 ∧ t.sname = s.sname) }SELECT DISTINCT sname
FROM Student
WHERE age > 20;WHERE plays the role of σ and the SELECT list plays the role of π. The keyword DISTINCT is part of the match. Relational algebra uses set semantics, so projection removes duplicates. SQL uses bag semantics, so a plain SELECT sname returns Ravi twice. Without DISTINCT, this query gives three rows, but the algebra expression gives two.
NULLs in a selection
Textbook relational algebra has no NULL. SQL does, and WHERE keeps a row only when the condition evaluates to TRUE. A comparison with NULL evaluates to UNKNOWN, so that row is dropped. If a student had age = NULL, the row would appear in neither age > 20 nor NOT (age > 20). Chapter content on three-valued logic covers this in full. For now, remember that σ and WHERE select only TRUE rows.
Counting results
GATE questions often ask for the number of tuples a query returns, or a minimum and maximum over all valid instances. Use these steps:
- Count the tuples that pass the selection. That count is at most |R|.
- For projection, count distinct combinations of the kept attributes. That count is at most the number of tuples that passed.
- For a minimum, ask whether the selection can reject every tuple (minimum 0, if no constraint forbids it) or whether a key forces a lower bound.
- For a maximum, use the number of tuples that passed when the kept attributes include a key. Otherwise the maximum is still that number, reached when all values are distinct.
For example, suppose R has 100 tuples and attribute A has at most 5 possible values. Then |π_A(R)| is at most 5, and at least 1 if R is nonempty. A selection before the projection can only lower these numbers. It can never raise them.
Common errors
- Writing projection before a selection that needs a dropped attribute.
- Forgetting that π removes duplicates, and so miscounting by using the input size.
- Forgetting
DISTINCTin SQL when the question asks for equivalence with an algebra expression. - Treating σ as choosing columns and π as choosing rows. The names are easy to swap. σ chooses rows, and π chooses columns.
Exercise
Let R(A, B, C) have 50 tuples, where A is the primary key and B takes at most 4 distinct values. What are the minimum and maximum numbers of tuples in πB(σC > 10 (R)) over all instances? Give the answer assuming no other constraints.
Show answer
The minimum is 0, because the selection can reject every tuple. The maximum is 4, because the projection keeps only B, and B has at most 4 distinct values. The primary key A does not matter here, since A is not in the projection list.
Exercise
Using the Student instance in this chapter, how many rows does SELECT sname FROM Student WHERE age >= 21; return, and how many tuples does πsname (σage >= 21 (Student)) return? Why do the counts differ?
Show answer
The SQL query returns 3 rows (Ravi, Meera, Ravi), because plain SELECT keeps duplicates. The algebra expression returns 2 tuples (Ravi, Meera), because relations are sets and projection removes duplicates. Adding DISTINCT to the SQL query makes both counts equal 2.