Relational Algebra
Relational algebra is a procedural query language. Each operation takes relations as input and produces a relation.
| Operation | Symbol | Meaning | SQL equivalent |
|---|---|---|---|
| Selection | σ | Filter rows | WHERE |
| Projection | π | Choose columns (removes duplicates) | SELECT DISTINCT cols |
| Union | ∪ | Rows in either | UNION |
| Set difference | − | Rows in R but not S | EXCEPT |
| Cartesian product | × | All combinations | CROSS JOIN |
| Rename | ρ | Rename relation/attributes | AS |
| Intersection | ∩ | Rows in both | INTERSECT |
| Natural join | ⋈ | Join on common attributes | NATURAL JOIN |
| Division | ÷ | "for all" queries | NOT EXISTS … NOT EXISTS |
The first six (σ, π, ∪, −, ×, ρ) are the fundamental operations; the rest can be derived from them.
Examples
Employee(eid, name, salary, dno), Dept(dno, dname)
1. Names of employees earning more than 50000:
π name ( σ salary > 50000 (Employee) )
2. Names of employees working in 'CSE':
π name ( σ dname = 'CSE' (Employee ⋈ Dept) )
3. Students who have taken ALL courses:
π roll, cid (Enrolls) ÷ π cid (Course)
Union Compatibility
For ∪, ∩ and −, both relations must have the same number of attributes with compatible domains.
Track your progress through the DBMS course