DBMS slides 📂 Introduction · 7 of 11 45 min read

Relational Model & Relational Algebra: Select, Project, Join, Divide

An 18-slide visual guide to the relational model and relational algebra — the math beneath SQL. It covers relation anatomy and keys, then every operator (σ select, π project, ∪ ∩ − set ops, ⋈ join, ÷ division) as animated input→output table transformations with worked examples, SQL equivalents, operator precedence and query-optimization equivalence rules.

⚙️

Relational Model & Relational Algebra

The math beneath SQL — relations in, relations out. Select, Project, Union, Join and Divide, how they chain, and how query optimizers rewrite them.
σ Select · π Project ∪ Union ⋈ Join ÷ Divide

Press Next → or use ← → arrow keys

Section 01

The Story Behind the Relational Model

1970 — Codd throws away the pointers
In 1970, E. F. Codd proposed storing data in plain tables instead of tangled pointer chains. That became the relational model — the foundation of every modern database. Its query engine is relational algebra: operations that take tables in and give tables out.
🔗
The Closure Property

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.

Section 02

Anatomy of a Relation

Formal TermEveryday TermMeaning
RelationTableA named set of rows with fixed columns
TupleRow / recordOne entry in the relation
AttributeColumn / fieldA named property
DomainData type / allowed valuesThe legal values for an attribute
DegreeNumber of columnsSTUDENT has degree 4
CardinalityNumber of rowsSTUDENT has cardinality 4
🎲
A Relation Is a Set

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.

Section 03

Schema, Instance & Keys

📄
Schema — the design
The permanent structure: STUDENT(Roll, Name, Age, Dept). Changes rarely.
📋
Instance — the data
The actual tuples right now: {101 Raj 22 CSE, 102 Sara 19 ECE, …}. Changes with every insert/update/delete.
🔑
Keys
Super ⊇ Candidate ⊇ Primary. A foreign key references another relation's PK — ENROLLED.Roll → STUDENT.Roll.
Key TypeDefinitionExample
Super KeyAny unique attribute set (extras allowed){Roll}, {Roll, Name}
Candidate KeyMinimal super key{Roll}, {Email}
Primary KeyThe chosen candidate keyRoll
Foreign KeyReferences another relation's PKENROLLED.Roll → STUDENT.Roll
Section 04

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.

OperationSymbolTypeMeaning
SelectionσUnaryPick rows by a condition
ProjectionπUnaryPick columns
UnionBinary / setAll rows of either relation
IntersectionBinary / setRows in both relations
DifferenceBinary / setRows in one but not the other
Cartesian Product×BinaryAll combinations of rows
JoinBinaryCombine related rows
RenameρUnaryRename relation / attributes
Division÷BinaryRows matching all of another set
Section 05

Selection (σ) — Pick the Rows

σAge > 20 ( STUDENT )  → slices horizontally
STUDENT (input) RollNameAgeDept 101Raj22CSE 102Sara19ECE 103Amit24CSE 104Neha20ECE σAge > 20 Output RollNameAgeDept 101Raj22CSE 103Amit24CSE
SELECT * FROM STUDENT WHERE Age > 20;   -- SQL equivalent
Section 06

Projection (π) — Pick the Columns

πDept ( STUDENT )  → slices vertically, then drops duplicates
STUDENT (input) RollNameAgeDept 101Raj22CSE 102Sara19ECE 103Amit24CSE 104Neha20ECE πDept Output — duplicates gone Dept CSE ECE 4 students → 2 departments
SELECT DISTINCT Dept FROM STUDENT;   -- DISTINCT mimics the set behaviour
Section 07

Union (∪), Intersection (∩) & Difference (−)

CRICKET Name RajSara CHESS Name SaraAmit CHESS PLAYERS = CRICKET ∪ CHESS Name Raj Sara Amit Sara appears once, not twice
∪ Union
Raj, Sara, Amit — everyone in either.
∩ Intersection
Sara only — in both relations.
− Difference
Cricket − Chess = Raj — first but not second.
⚖️
Union-Compatibility Required

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.

Section 08

Join (⋈) — Combine Related Rows

STUDENT ENROLLED  → match on the common attribute Roll
STUDENT RollName 101Raj 103Amit ENROLLED RollCID 101→C1,C2 STUDENT ⋈ ENROLLED RollNameDeptCID 101RajCSEC1 101RajCSEC2 103AmitCSEC1
Natural Join
Matches all common attributes; keeps one copy of the shared column.
Equi Join
Joins on an equality condition (θ is =).
Theta Join
Joins on any condition (<, >, ≠, …).
Section 09

Division (÷) — The "For All" Operator

ENROLLED ÷ COURSE  → "which students took every course?"
ENROLLED RollCID 101C1 101C2 102C1 103C1 103C2 COURSE ÷ CID C1C2 took C1 AND C2 Roll 101 103 102 missing C2 ✗
The Logic

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.

Section 10

Putting It Together — Nesting Operations

"Names of CSE students older than 20 who are enrolled in something."

πName ( σAge>20 ∧ Dept='CSE' ( STUDENT ENROLLED ) )
STUDENT ⋈ ENROLLED σ Age>20 ∧ CSE π Name read inside-out: join → filter rows → keep only the Name column
SELECT DISTINCT S.Name
FROM STUDENT S JOIN ENROLLED E ON S.Roll = E.Roll
WHERE S.Age > 20 AND S.Dept = 'CSE';
Section 11 · A & B

Precedence & Two Ways to Write a Query

PriorityOperators
Highestσ π ρ
Then× ⋈
Then
Lowest∪ −

Parentheses override precedence and clarify intent.

// nested (compact)
πName( σAge>20(STUDENT) ENROLLED )
// sequential (readable)
T1 ← σAge>20(STUDENT)
T2 ← T1 ENROLLED
Result ← πName(T2)
🟰
Same Result, Two Styles

Nest it for short queries; assign step-by-step temporaries for readable ones. Both compute identical relations.

Section 11 · C

Equivalence Rules — How Optimizers Rewrite Queries

RuleEquivalenceBenefit
Cascade of σσc1∧c2(R) ≡ σc1c2(R))Split / merge conditions
Commute σσc1c2(R)) ≡ σc2c1(R))Apply the cheaper filter first
Cascade of ππL1L2(R)) ≡ πL1(R) if L1 ⊆ L2Drop redundant projections
Product + σ = Joinσθ(R × S) ≡ R ⋈θ SConvert slow product to join
Selection push-downσc(R ⋈ S) ≡ σc(R) ⋈ SFilter before joining
Commute ⋈ / ∪R ⋈ S ≡ S ⋈ RReorder for a better plan
The Golden Optimization

Selection push-down — filter early, shrinking the number of rows before an expensive join. This single idea drives much of real query optimization.

Section 11 · D

Worked Example — Combine Two Branches

"Names who are either in ECE, OR over 20 and enrolled."

πName(σDept='ECE'(STUDENT)) πName(σAge>20(STUDENT) ENROLLED)
Branch 1 — ECE students
σDept='ECE' → {Sara, Neha} → πName{Sara, Neha}
Branch 2 — older & enrolled
σAge>20 → {Raj, Amit}, join ENROLLED, πName{Raj, Amit}
🔀
Final Union

{ Sara, Neha, Raj, Amit } — the union stacks both branches and drops any duplicate name automatically.

Section 12 · Cheat Sheet

Relational Algebra ↔ SQL

OperationAlgebraSQL
SelectionσAge>20(R)WHERE Age > 20
ProjectionπName(R)SELECT DISTINCT Name
UnionR ∪ S... UNION ...
IntersectionR ∩ SINTERSECT
DifferenceR − SEXCEPT / MINUS
JoinR ⋈ SJOIN ... ON / NATURAL JOIN
DivisionR ÷ SGROUP BY … HAVING COUNT / double NOT EXISTS
🔁
The Irony

SQL's keyword SELECT actually performs projection (π), not selection (σ). The algebra's "selection" is SQL's WHERE.

Section 13

Three Common Mistakes to Avoid

↔️
Selection vs Projection
σ keeps rows (horizontal); π keeps columns (vertical). Ironically, SQL's SELECT does projection.
👥
Projection drops duplicates
Algebra treats relations as sets, so π silently removes repeats. In SQL you must add DISTINCT.
⚖️
Union without compatibility
Can't union a 2-column relation with a 3-column one, or mix domains. Same arity, same order, matching domains.
🧭
Remember the Shapes

σ is a horizontal slice, π is a vertical slice — and both outputs are sets, so duplicates never survive.

Section 14

Golden Rules of Relational Algebra

🏆 NON-NEGOTIABLE RULES
1
Every operator takes relations and returns a relation. This closure is what enables nesting.
2
σ works on rows, π works on columns. Selection is horizontal; projection is vertical.
3
Results are sets, so duplicates vanish. Projection and union eliminate repeated tuples automatically.
4
Set operations need union-compatibility. Union, intersection and difference require identical structure.
5
Join needs a common attribute; division answers "for all." Combine with ⋈; use ÷ for "every" questions.
6
Relational algebra is SQL's blueprint. Understanding it makes SQL easier to reason about and to optimize.
FINAL

The Math Beneath SQL

σ πRows & columns
∪ ∩ −Set operations
Join
÷"For all"
1970Codd's model
🎯
The Foundation Is Set

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.

🧠
One Sentence to Remember

σ 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