Relational Model & Relational Algebra
Press Next → or use ← → arrow keys
The Story Behind the Relational Model
A relation is a table; relational algebra is a set of operations where every input is a relation and every output is a relation — so results can feed into more operations, like links in a chain.
Anatomy of a Relation
| Formal Term | Everyday Term | Meaning |
|---|---|---|
| Relation | Table | A named set of rows with fixed columns |
| Tuple | Row / record | One entry in the relation |
| Attribute | Column / field | A named property |
| Domain | Data type / allowed values | The legal values for an attribute |
| Degree | Number of columns | STUDENT has degree 4 |
| Cardinality | Number of rows | STUDENT has cardinality 4 |
Because it's a set, no two tuples are identical and order doesn't matter. This one fact explains why projection and union silently drop duplicates.
Schema, Instance & Keys
| Key Type | Definition | Example |
|---|---|---|
| Super Key | Any unique attribute set (extras allowed) | {Roll}, {Roll, Name} |
| Candidate Key | Minimal super key | {Roll}, {Email} |
| Primary Key | The chosen candidate key | Roll |
| Foreign Key | References another relation's PK | ENROLLED.Roll → STUDENT.Roll |
Relational Algebra — The Operators
It's procedural: you specify the operations and their order. Every operator consumes one or two relations and returns a relation.
| Operation | Symbol | Type | Meaning |
|---|---|---|---|
| Selection | σ | Unary | Pick rows by a condition |
| Projection | π | Unary | Pick columns |
| Union | ∪ | Binary / set | All rows of either relation |
| Intersection | ∩ | Binary / set | Rows in both relations |
| Difference | − | Binary / set | Rows in one but not the other |
| Cartesian Product | × | Binary | All combinations of rows |
| Join | ⋈ | Binary | Combine related rows |
| Rename | ρ | Unary | Rename relation / attributes |
| Division | ÷ | Binary | Rows matching all of another set |
Selection (σ) — Pick the Rows
SELECT * FROM STUDENT WHERE Age > 20; -- SQL equivalent
Projection (π) — Pick the Columns
SELECT DISTINCT Dept FROM STUDENT; -- DISTINCT mimics the set behaviour
Union (∪), Intersection (∩) & Difference (−)
All three need the same number of attributes, in the same order, with matching domains — you can't union a 2-column relation with a 3-column one.
Join (⋈) — Combine Related Rows
=).Division (÷) — The "For All" Operator
Roll 101 has {C1, C2} ✓ · Roll 102 has only {C1}, missing C2 ✗ · Roll 103 has {C1, C2} ✓. Division answers "for all / every" questions — the one operator SQL has no direct keyword for.
Putting It Together — Nesting Operations
"Names of CSE students older than 20 who are enrolled in something."
SELECT DISTINCT S.Name
FROM STUDENT S JOIN ENROLLED E ON S.Roll = E.Roll
WHERE S.Age > 20 AND S.Dept = 'CSE';
Precedence & Two Ways to Write a Query
| Priority | Operators |
|---|---|
| Highest | σ π ρ |
| Then | × ⋈ |
| Then | ∩ |
| Lowest | ∪ − |
Parentheses override precedence and clarify intent.
πName( σAge>20(STUDENT) ⋈ ENROLLED )
T1 ← σAge>20(STUDENT)
T2 ← T1 ⋈ ENROLLED
Result ← πName(T2)
Nest it for short queries; assign step-by-step temporaries for readable ones. Both compute identical relations.
Equivalence Rules — How Optimizers Rewrite Queries
| Rule | Equivalence | Benefit |
|---|---|---|
| Cascade of σ | σc1∧c2(R) ≡ σc1(σc2(R)) | Split / merge conditions |
| Commute σ | σc1(σc2(R)) ≡ σc2(σc1(R)) | Apply the cheaper filter first |
| Cascade of π | πL1(πL2(R)) ≡ πL1(R) if L1 ⊆ L2 | Drop redundant projections |
| Product + σ = Join | σθ(R × S) ≡ R ⋈θ S | Convert slow product to join |
| Selection push-down | σc(R ⋈ S) ≡ σc(R) ⋈ S | Filter before joining |
| Commute ⋈ / ∪ | R ⋈ S ≡ S ⋈ R | Reorder for a better plan |
Selection push-down — filter early, shrinking the number of rows before an expensive join. This single idea drives much of real query optimization.
Worked Example — Combine Two Branches
"Names who are either in ECE, OR over 20 and enrolled."
{ Sara, Neha, Raj, Amit } — the union stacks both branches and drops any duplicate name automatically.
Relational Algebra ↔ SQL
| Operation | Algebra | SQL |
|---|---|---|
| Selection | σAge>20(R) | WHERE Age > 20 |
| Projection | πName(R) | SELECT DISTINCT Name |
| Union | R ∪ S | ... UNION ... |
| Intersection | R ∩ S | INTERSECT |
| Difference | R − S | EXCEPT / MINUS |
| Join | R ⋈ S | JOIN ... ON / NATURAL JOIN |
| Division | R ÷ S | GROUP BY … HAVING COUNT / double NOT EXISTS |
SQL's keyword SELECT actually performs projection (π), not selection (σ). The algebra's "selection" is SQL's WHERE.
Three Common Mistakes to Avoid
DISTINCT.σ is a horizontal slice, π is a vertical slice — and both outputs are sets, so duplicates never survive.
Golden Rules of Relational Algebra
The Math Beneath SQL
Relations in, relations out — that closure lets you chain Select, Project, Union, Join and Divide into any query, and lets optimizers rewrite them with equivalence rules. Every SQL statement you'll ever write is this algebra in friendlier clothing. Next: SQL itself, and beyond it, normalization.
σ slices rows, π slices columns, ⋈ stitches tables on a shared value, ÷ answers "for all" — and every result is another relation you can feed back in.
⚙️ End of tutorial · Press ← to review, or click Restart