Relational Algebra: Selection & Projection
Selection chooses rows; projection chooses columns. They are the first two moves in almost every query plan.
In plain English
Selection picks rows; projection picks columns. It is like choosing which students to show and which facts about them.
The one idea
Selection filters rows; projection chooses columns. Selection preserves the schema, projection can reduce the degree.
Every query plan begins by pushing selections and projections closer to the data. Understanding the operators makes EXPLAIN output readable.
By the end
- 01Apply selection σ with a predicate.
- 02Apply projection π and know when duplicates disappear.
- 03Compose selection and projection.
- 04Read relational algebra notation.
Selection σ
Selection filters tuples that satisfy a predicate. σ_age ≥ 21 (Student) returns only students aged 21 or older. Selection does not change the schema; it returns the same columns with fewer rows.
Student(id, name, age)
1 Ada 21
2 Grace 22
3 Linus 20
σ_age >= 21 (Student) -> Ada, GraceProjection π
Projection chooses a subset of attributes. π_name, age (Student) returns only the name and age columns. Because a relation is a set, duplicate rows are eliminated. In SQL, SELECT DISTINCT makes that explicit; SELECT without DISTINCT uses bag semantics.
π_name (Student) -> {Ada}, {Grace}, {Linus}
π_age (Student) -> {21}, {22}, {20}
/* if two students were 21, projection would return one 21 */Composition
Operators compose. π_name (σ_age ≥ 21 (Student)) first filters rows, then projects columns. The order matters for performance and sometimes for the result when duplicates are involved. The optimizer may reorder operations as long as the result is equivalent.
Names of students over 20
Student(id, name, age) has Ada 21, Grace 22, and Linus 20.
Apply σ_age > 20 to Student.
Ada and Grace survive; Linus is removed.
Apply π_name to the result.
The schema becomes (name) and the tuples are Ada and Grace.
Try π_name first, then σ_age > 20.
The predicate refers to a column that no longer exists, so the expression is invalid.
ConclusionThe order is not cosmetic. Projection can remove the attribute a later predicate needs.
Instrument
Watch it happen.
Step through one operation at a time
Start with Student
Selection keeps the rows that satisfy a predicate. The schema stays the same.
| id | name | age |
|---|---|---|
| 1 | Ada | 21 |
| 2 | Grace | 22 |
| 3 | Linus | 20 |
The trade-off
No free correctness.
Gain
Smaller intermediate relations and less I/O.
Cost
Projecting too early can force a rejoin or lose a key needed later.
Verdict
Push selections early; push projections only when the required columns are known.
Recap
What to keep.
Takeaways
- +Selection filters rows; projection filters columns.
- +Selection keeps the schema; projection changes it.
- +Projection eliminates duplicates under set semantics.
- +Operators can be composed and nested.
- +SQL SELECT is closer to projection, WHERE to selection.
Traps
- !Confusing selection with projection.
- !Forgetting duplicate elimination in projection.
- !Assuming SQL SELECT always removes duplicates; it does not without DISTINCT.
Focus
- →Write σ and π expressions for a small table.
- →Explain why projection can reduce cardinality.
- →Trace the order of operations in a composed expression.
Check yourself
Before you turn the page.
Which operator filters rows?
What does projection do?