Skip to content
Contents/CH 10
Relational Model/Beginner · 14 min

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

  1. 01Apply selection σ with a predicate.
  2. 02Apply projection π and know when duplicates disappear.
  3. 03Compose selection and projection.
  4. 04Read relational algebra notation.
01

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.

chapter-10.db
Student(id, name, age)
1 Ada 21
2 Grace 22
3 Linus 20

σ_age >= 21 (Student) -> Ada, Grace
02

Projection π

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.

chapter-10.db
π_name (Student) -> {Ada}, {Grace}, {Linus}

π_age (Student) -> {21}, {22}, {20}
/* if two students were 21, projection would return one 21 */
03

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.

Worked example3 steps

Names of students over 20

Student(id, name, age) has Ada 21, Grace 22, and Linus 20.

01

Apply σ_age > 20 to Student.

Ada and Grace survive; Linus is removed.

02

Apply π_name to the result.

The schema becomes (name) and the tuples are Ada and Grace.

03

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

Step player01 / 03
01

Start with Student

Selection keeps the rows that satisfy a predicate. The schema stays the same.

Student
idnameage
1Ada21
2Grace22
3Linus20

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.

0 / 2 answered
01

Which operator filters rows?

02

What does projection do?

DB//COSMOS

A field manual for database systems: model the domain, normalize the facts, query the relations, and protect the result.

Edition 2026 · 32 chaptersTopic catalogInterview prep