2.1Basic operators: σ, π, ∪, −, ×, ρ
The idea
Relational algebra is a small language of operators on tables. The core ones are σ (select rows), π (pick columns), ∪, −, × and ρ (rename).
Think of it like this: Spreadsheet tools: filter rows, hide columns, stack two sheets, subtract one sheet from another.
Why it exists
Every query language is judged against it, and exam questions ask what an expression returns.
How it works
- σ keeps the rows that satisfy a condition, all columns kept.
- π keeps some columns and removes duplicate rows.
- ∪, ∩, − need union-compatible tables (same number and types of columns).
- × pairs each row of R with each row of S: m·n rows.
Worked example: R has 4 rows, S has 3 rows. How many rows in R × S?
12 and the columns of both tables together.
Now play with it
- →Run each operator in the lab and compare the input tables with the result.
- →See how π collapses duplicate did values.
| eid | name | did |
|---|---|---|
| 1 | Asha | 10 |
| 2 | Ben | 20 |
| 3 | Chen | 10 |
| 4 | Dia | NULL |
| eid | name | did |
|---|---|---|
| 1 | Asha | 10 |
| 3 | Chen | 10 |
Keeps rows, keeps all columns. Rows with NULL never satisfy a comparison.
Check yourself
π_A(R) where R(A, B) = {(1,x), (1,y), (2,x)} returns how many rows?
Exam notes (the dense version)
σ selects rows, π picks columns (and removes duplicates), ∪ − ∩ need union-compatible relations, × pairs every row with every row, ρ renames. Relational algebra uses set semantics (no duplicates). Everything else (∩, joins, division) can be derived from σ π ∪ − ×.
Previous year questions: Relational Algebra & Calculus (12)
PYQs are grouped by topic, so this is the full set for Relational Algebra & Calculus; try the ones that match this subtopic. Open all 12 on the topic page.