🧮 RELATIONAL ALGEBRA
Complete Interactive Visual Learning Module
🌟 What is Relational Algebra?
Relational Algebra is a formal system of operations used to retrieve and manipulate information stored in relational database tables.
A relation can be thought of as a table containing rows (tuples) and columns (attributes).
For example: STUDENT may contain:
Student_ID, Name, Age, Course_ID
Relational Algebra allows us to ask questions such as:
👉 Which students are older than 18?
👉 Show only student names.
👉 Combine Student and Course information.
👉 Find students present in two different relations.
👉 Find students who completed ALL courses.
Relational Algebra is a formal system of operations used to retrieve and manipulate information stored in relational database tables.
A relation can be thought of as a table containing rows (tuples) and columns (attributes).
For example: STUDENT may contain:
Student_ID, Name, Age, Course_ID
Relational Algebra allows us to ask questions such as:
👉 Which students are older than 18?
👉 Show only student names.
👉 Combine Student and Course information.
👉 Find students present in two different relations.
👉 Find students who completed ALL courses.
📚 Example Database
STUDENT
| Student_ID | Name | Age | Course_ID |
|---|---|---|---|
| 101 | Rahul | 20 | 10 |
| 102 | Anita | 19 | 20 |
| 103 | Arjun | 22 | 30 |
| 104 | Riya | 17 | 40 |
COURSE
| Course_ID | Course_Name |
|---|---|
| 10 | BCA |
| 20 | BBA |
| 30 | B.Sc |
| 50 | MCA |
All major Relational Algebra operations are
available as interactive buttons above.
Click any button to learn its definition, formula and step-by-step example.
Click any button to learn its definition, formula and step-by-step example.
σ Selection
Selection chooses specific
ROWS from a relation according to a condition.
Think: "Which rows do I want?"
Think: "Which rows do I want?"
σAge > 18(STUDENT)
1
Read the first student: Rahul, Age = 20.
2
Check 20 > 18 → TRUE → Keep Rahul.
3
Anita: 19 > 18 → TRUE → Keep Anita.
4
Arjun: 22 > 18 → TRUE → Keep Arjun.
5
Riya: 17 > 18 → FALSE → Remove Riya.
Result:
Rahul, Anita and Arjun.
🧠 σ = ROWS
🧠 σ = ROWS
π Projection
Projection selects specific
COLUMNS from a relation.
πName, Age(STUDENT)
1
Start with the STUDENT relation.
2
Select only Name and Age columns.
3
Ignore Student_ID and Course_ID.
Result:
🧠 π = COLUMNS
| Name | Age |
|---|---|
| Rahul | 20 |
| Anita | 19 |
| Arjun | 22 |
| Riya | 17 |
∪ Union
Union combines tuples from two union-compatible relations.
Duplicate tuples are removed.
A ∪ B
A = {Rahul, Anita, Arjun}
B = {Anita, Riya}
A ∪ B
= {Rahul, Anita, Arjun, Riya}
B = {Anita, Riya}
A ∪ B
= {Rahul, Anita, Arjun, Riya}
🎯 Union means:
ALL records from A and B without duplicates.
− Set Difference
Difference returns tuples present in the first relation
but not in the second relation.
A − B
A = {Rahul, Anita, Arjun}
B = {Anita, Arjun}
A − B
= {Rahul}
B = {Anita, Arjun}
A − B
= {Rahul}
🎯 Rahul exists in A but not in B.
∩ Intersection
Intersection returns only tuples common to both relations.
A ∩ B
A = {Rahul, Anita, Arjun}
B = {Anita, Arjun, Riya}
A ∩ B
= {Anita, Arjun}
B = {Anita, Arjun, Riya}
A ∩ B
= {Anita, Arjun}
🎯 Intersection = COMMON records
× Cartesian Product
Cartesian Product combines every row of one relation
with every row of another relation.
STUDENT × COURSE
1
Suppose STUDENT has 2 rows.
2
Suppose COURSE has 3 rows.
3
Every student is combined with every course.
Number of rows = 2 × 3 = 6
🎯 Cartesian Product = EVERY POSSIBLE COMBINATION
ρ Rename
Rename changes the name of a relation or attributes.
ρS(STUDENT)
1
Original relation name = STUDENT.
2
Apply ρ operator.
3
New relation name = S.
STUDENT → S
The actual data does not change.
The actual data does not change.
θ Theta Join
Theta Join uses a comparison condition between two relations.
R ⋈θ S
Possible comparison operators:
= ≠ < > ≤ ≥
= ≠ < > ≤ ≥
Example:
STUDENT.Course_ID < COURSE.Course_ID
This is a Theta Join.
STUDENT.Course_ID < COURSE.Course_ID
This is a Theta Join.
= Equi Join
Equi Join is a join whose condition uses the
equality operator (=).
STUDENT ⋈
STUDENT.Course_ID = COURSE.Course_ID
COURSE
1
Compare Student Course_ID with Course Course_ID.
2
10 = 10 → Match.
3
20 = 20 → Match.
4
30 = 30 → Match.
Equi Join = Join using equality (=)
🌿 Natural Join
Natural Join automatically uses common attributes
with compatible domains.
Duplicate join attributes are represented only once.
STUDENT ⋈ COURSE
1
Find common attribute names.
2
Course_ID exists in both tables.
3
Match equal Course_ID values.
4
Combine matching rows.
Natural Join automatically identifies the common
attribute.
🔗 Inner Join
Returns only rows that have matching values in both tables.
STUDENT ⋈ COURSE
| Name | Course_ID | Course_Name |
|---|---|---|
| Rahul | 10 | BCA |
| Anita | 20 | BBA |
| Arjun | 30 | B.Sc |
Only matching Course_ID values are returned.
⬅ Left Outer Join
Returns all rows from the LEFT relation and
matching rows from the RIGHT relation.
Unmatched right-side attributes become NULL.
STUDENT ⟕ COURSE
| Name | Course_ID | Course_Name |
|---|---|---|
| Rahul | 10 | BCA |
| Anita | 20 | BBA |
| Arjun | 30 | B.Sc |
| Riya | 40 | NULL |
⬅ LEFT JOIN = ALL LEFT RECORDS
➡ Right Outer Join
Returns all rows from the RIGHT relation and
matching rows from the LEFT relation.
STUDENT ⟖ COURSE
| Name | Course_ID | Course_Name |
|---|---|---|
| Rahul | 10 | BCA |
| Anita | 20 | BBA |
| Arjun | 30 | B.Sc |
| NULL | 50 | MCA |
➡ RIGHT JOIN = ALL RIGHT RECORDS
↔ Full Outer Join
Returns all rows from both relations.
Matching records are combined.
Unmatched records receive NULL values.
STUDENT ⟗ COURSE
| Name | Course_ID | Course_Name |
|---|---|---|
| Rahul | 10 | BCA |
| Anita | 20 | BBA |
| Arjun | 30 | B.Sc |
| Riya | 40 | NULL |
| NULL | 50 | MCA |
↔ FULL JOIN = ALL RECORDS FROM BOTH TABLES
👤 Self Join
Self Join joins a relation with itself.
It is useful when rows in the same table are related
to one another.
EMPLOYEE
| Emp_ID | Employee | Manager_ID |
|---|---|---|
| 101 | Rahul | 103 |
| 102 | Anita | 103 |
| 103 | Arjun | NULL |
E1 ⋈E1.Manager_ID = E2.Emp_ID E2
Result:
Rahul → Arjun
Anita → Arjun
Here Arjun is the manager of Rahul and Anita.
Rahul → Arjun
Anita → Arjun
Here Arjun is the manager of Rahul and Anita.
✖ Cross Join
Cross Join creates the Cartesian Product of two relations.
STUDENT × COURSE
Suppose:
STUDENT = 3 rows
COURSE = 4 rows
Number of result rows:
3 × 4 = 12
STUDENT = 3 rows
COURSE = 4 rows
Number of result rows:
3 × 4 = 12
Every student is paired with every course.
÷ Division
Division is used when a query contains the idea of
"ALL".
STUDENT_COURSE ÷ REQUIRED_COURSE
Required Courses
| Course_ID |
|---|
| BCA101 |
| BCA102 |
| BCA103 |
Rahul completed:
BCA101 ✓
BCA102 ✓
BCA103 ✓
Anita completed:
BCA101 ✓
BCA102 ✓
BCA103 ✗
BCA101 ✓
BCA102 ✓
BCA103 ✓
Anita completed:
BCA101 ✓
BCA102 ✓
BCA103 ✗
Division result:
Rahul
Because Rahul completed ALL required courses.
Rahul
Because Rahul completed ALL required courses.
❓ Extra BIT Questions & Answers
Click any question to reveal its answer.
Q1. What is Relational Algebra?
Relational Algebra is a formal procedural language used
to perform operations on relational database relations.
Q2. Which operator selects rows?
Selection (σ) selects rows satisfying a condition.
Q3. Which operator selects columns?
Projection (π) selects columns from a relation.
Q4. What is the difference between Selection and Projection?
Selection works mainly on rows, while Projection works
mainly on columns.
σ → Rows
π → Columns
σ → Rows
π → Columns
Q5. What is Union?
Union combines tuples from two union-compatible relations
and removes duplicate tuples.
Q6. What is Cartesian Product?
Cartesian Product combines every tuple of one relation
with every tuple of another relation.
Q7. If A has 5 rows and B has 4 rows, how many rows can A × B produce?
5 × 4 = 20 rows.
Q8. What is Natural Join?
Natural Join automatically joins relations using common
attributes with compatible domains.
Q9. What is an Equi Join?
Equi Join uses equality (=) as the join condition.
Q10. What is a Theta Join?
Theta Join uses comparison operators such as
=, ≠, <, >, ≤ and ≥.
Q11. What does Left Outer Join return?
It returns all rows from the left relation and matching
rows from the right relation.
Q12. What does Right Outer Join return?
It returns all rows from the right relation and matching
rows from the left relation.
Q13. What does Full Outer Join return?
It returns all rows from both relations, including
unmatched rows.
Q14. What is Self Join?
Self Join joins a relation with itself, usually using
different aliases.
Q15. What is Division used for?
Division is useful for queries involving the concept
of "ALL".
Q16. Which operator renames a relation?
ρ (Rename).
Q17. What is the result of A ∩ B?
The result contains tuples common to both A and B.
Q18. What does A − B mean?
It returns tuples present in A but not present in B.
Q19. Which operation produces every possible combination?
Cartesian Product / Cross Join (×).
Q20. Which JOIN keeps all records from both tables?
Full Outer Join.
🎯 COMPLETE RELATIONAL ALGEBRA REVISION
σ Selection π Projection ∪ Union − Difference ∩ Intersection × Cartesian Product ρ Rename θ Theta Join Equi Join Natural Join Inner Join Left Join Right Join Full Join Self Join Cross Join ÷ Division
σ = ROWS | π = COLUMNS | × = COMBINATIONS
σ Selection π Projection ∪ Union − Difference ∩ Intersection × Cartesian Product ρ Rename θ Theta Join Equi Join Natural Join Inner Join Left Join Right Join Full Join Self Join Cross Join ÷ Division
σ = ROWS | π = COLUMNS | × = COMBINATIONS
No comments:
Post a Comment