Part 2 of 5 · 1 min read
Selection and projection
Keeping rows that match a condition, and keeping only some columns.
Think of a photograph album. Selection picks out only the photos from one trip. Projection then crops every kept photo down to just one corner of each.
In plain words: selection, written σ (sigma), keeps only the rows of a relation that satisfy a condition. It does not change which columns exist. Projection, written π (pi), keeps only the named columns of a relation, across every row. Selection narrows rows; projection narrows columns.
A worked example. Selecting only orders for customer 107, then projecting down to just the order ID and date.
The key lines. The algebra, then the SQL it corresponds to:
π order_id, placed_on (σ customer_id = 107 (orders))
SELECT order_id, placed_on FROM orders WHERE customer_id = 107;
Selection, projection
Selection (σ) returns the rows of a relation satisfying a given condition. Projection (π) returns only the named columns of a relation, for every row.
Example: σ customer_id = 107 (orders) is selection; wrapping it in π order_id, placed_on (...) adds projection on top.