Relational algebra is a set of operations that take tables and give back tables. It is the theory behind SQL, so understanding it makes queries easier to write and to optimise.
Sample tables
student(roll_no, name, dept_id, marks) and department(dept_id, dept_name).
Basic operations
| Operation | Symbol | Meaning | Example |
|---|---|---|---|
| Selection | σ | Keeps rows that satisfy a condition | σ marks > 60 (student) |
| Projection | π | Keeps chosen columns, removes duplicates | π name, marks (student) |
| Union | ∪ | Rows in either table | A ∪ B |
| Intersection | ∩ | Rows in both | A ∩ B |
| Difference | − | Rows in A but not B | A − B |
| Cartesian product | × | Every row of A paired with every row of B | A × B |
| Rename | ρ | Gives a new name | ρ s (student) |
Union, intersection and difference need union-compatible tables: the same number of columns with matching domains.
Join and division
- Join (⋈): a Cartesian product followed by a selection. A natural join matches columns with the same name; an equi join matches on equality of chosen columns; an outer join also keeps unmatched rows.
- Division (÷): answers "for all" questions, such as "students who took all the courses offered by CS". If
takes(roll_no, course_id)is divided bycs_courses(course_id), the result holds the roll numbers paired with every course incs_courses.
Worked example
Names of students of the CS department who scored above 60.
π name ( σ marks > 60 ∧ dept_name = 'CS' ( student ⋈ department ) )
The join combines the tables, the selection filters rows, and the projection keeps only the name.
Relational calculus
Calculus says what you want, not how to get it.
- Tuple relational calculus (TRC): variables stand for whole rows.
{ t | t ∈ student ∧ t.marks > 60 }means "all student rows with marks above 60". - Domain relational calculus (DRC): variables stand for column values.
{ (n, m) | ∃ r ((r, n, d, m) ∈ student ∧ m > 60) }gives name and marks pairs.
Algebra and safe calculus can express exactly the same queries.
Common mistakes
- Using σ when you meant π. Selection picks rows; projection picks columns.
- Applying union to tables that are not union-compatible.
- Forgetting that projection removes duplicates in pure relational algebra, while SQL keeps them unless you write DISTINCT.
Practice questions
- Differentiate selection and projection.
- What does union-compatible mean?
- Write relational algebra for "names of students with marks above 75".
- What is the division operation used for?
- Differentiate tuple and domain relational calculus.
Answers to the practice questions
- Selection (σ) filters rows; projection (π) chooses columns.
- The two tables have the same number of columns with compatible domains.
- π name ( σ marks > 75 ( student ) ).
- For queries that say "for all", such as students enrolled in all courses of a department.
- TRC variables range over rows; DRC variables range over single column values.
Sources
- Official BCA syllabus Database Management Systems, course code CC202, Unit II
- Raghu Ramakrishnan and Johannes Gehrke, Database Management Systems, 3rd edition, McGraw-Hill
