Evaluate works_on[emp_id, proj_id] ÷ project[proj_id] on the sample data using only basic operators, and name the employee(s) found.
Relational algebra is the procedural query language underneath SQL: select, project, rename, union, difference and Cartesian product are the six basic operators, and joins, intersection, division and grouping are built from them. Tuple and domain relational calculus express the same queries declaratively.
Relational algebra, defined by Codd alongside the relational model, is a set of operators that take one or two relations as input and return a new relation. Because the output is always a relation (the closure property), operators can be nested freely, like arithmetic expressions. Algebra is procedural: an expression spells out the sequence of operations, which is why optimizers use it internally to represent query plans.
All examples in this module use the same four tables, which are also loaded in the SQL playground on this page. Notation: σ[condition](R), π[attribute list](R) and ρ[new name](R) — the square brackets hold what textbooks print as a subscript.
| dept_id | dname | location |
|---|---|---|
| 10 | Accounts | Thrissur |
| 20 | Research | Kochi |
| 30 | Sales | Kozhikode |
| 40 | Operations | Kochi |
| emp_id | ename | job | salary | hire_date | dept_id | manager_id |
|---|---|---|---|---|---|---|
| 101 | Anil | President | 90000 | 2015-01-10 | 10 | NULL |
| 102 | Bindu | Manager | 60000 | 2016-03-15 | 20 | 101 |
| 103 | Charles | Manager | 55000 | 2017-06-01 | 30 | 101 |
| 104 | Divya | Analyst | 50000 | 2018-07-20 | 20 | 102 |
| 105 | Eby | Analyst | 50000 | 2019-02-11 | 20 | 102 |
| 106 | Fathima | Salesman | 30000 | 2019-09-05 | 30 | 103 |
| 107 | Gopal | Salesman | 28000 | 2020-01-17 | 30 | 103 |
| 108 | Hari | Clerk | 22000 | 2021-04-12 | 10 | 101 |
| 109 | Indu | Clerk | 20000 | 2022-08-30 | 20 | 104 |
| 110 | Jose | Clerk | 25000 | 2023-11-02 | NULL | 103 |
| proj_id | pname | dept_id | budget |
|---|---|---|---|
| 1 | Payroll | 10 | 400000 |
| 2 | AI Lab | 20 | 900000 |
| 3 | CRM | 30 | 500000 |
| 4 | Website | 20 | 300000 |
| emp_id | proj_id | hours |
|---|---|---|
| 102 | 1 | 10 |
| 102 | 2 | 20 |
| 102 | 3 | 10 |
| 102 | 4 | 10 |
| 104 | 2 | 30 |
| 104 | 4 | 20 |
| 105 | 1 | 20 |
| 105 | 2 | 20 |
| 105 | 4 | 10 |
| 106 | 3 | 40 |
| 107 | 3 | NULL |
| 108 | 1 | 30 |
Select σ[P](R) returns the tuples of R that satisfy predicate P — a horizontal subset with the same attributes. P may compare attributes with constants or with each other using =, ≠, <, ≤, >, ≥ and combine them with ∧ (and), ∨ (or), ¬ (not). σ[salary > 50000](emp) returns Anil, Bindu and Charles: 3 tuples, still 7 attributes.
Project π[A₁, …, Aₖ](R) keeps only the listed attributes — a vertical subset — and, because a relation is a set, removes duplicate tuples. π[job](emp) turns 10 rows into just 5: President, Manager, Analyst, Salesman, Clerk. SQL's SELECT does not remove duplicates unless you write DISTINCT; that difference is a classic exam point.
Rename ρ[S](R) gives the result a new name, and ρ[S(B₁, …, Bₙ)](R) also renames its attributes. It is needed whenever a relation is combined with itself, for example to compare each employee with his or her manager: ρ[e](emp) ⋈[e.manager_id = m.emp_id] ρ[m](emp).
Union R ∪ S, intersection R ∩ S and difference R − S behave as in set theory, but only for union-compatible relations: both must have the same degree, and the i-th attribute of each must come from the same domain. Attribute names may differ; the result takes the names of the first operand.
| Expression | Meaning | Result on the sample data |
|---|---|---|
| P1 = π[emp_id](σ[proj_id = 1](works_on)) | Employees on Payroll | {102, 105, 108} |
| P2 = π[emp_id](σ[proj_id = 2](works_on)) | Employees on AI Lab | {102, 104, 105} |
| P1 ∪ P2 | On Payroll or AI Lab | {102, 104, 105, 108} |
| P1 ∩ P2 | On both | {102, 105} |
| P1 − P2 | On Payroll but not AI Lab | {108} |
| P2 − P1 | On AI Lab but not Payroll | {104} |
∪ and ∩ are commutative and associative; − is neither (P1 − P2 ≠ P2 − P1). Intersection is not fundamental because R ∩ S = R − (R − S).
The Cartesian product R × S pairs every tuple of R with every tuple of S. Its degree is deg(R) + deg(S) and its cardinality is exactly |R|·|S|. emp × dept has 10 × 4 = 40 tuples and 7 + 3 = 10 attributes. On its own it is rarely useful; followed by a selection that matches related tuples, it becomes a join.
A theta join R ⋈[θ] S is shorthand for σ[θ](R × S), where θ is any comparison between attributes of R and S. When θ uses only equality it is an equi-join. The natural join R ⋈ S equates every pair of attributes with the same name and keeps one copy of each such attribute.
emp ⋈ dept joins on their only common attribute, dept_id, giving 9 tuples with 7 + 3 − 1 = 9 attributes. Jose drops out because his dept_id is NULL, and Operations drops out because no employee matches it. Two edge cases are worth memorising: with no common attributes a natural join is the Cartesian product; with all attributes common it is the intersection.
| Operator | Keeps | On emp and dept |
|---|---|---|
| emp ⋈ dept (natural / inner) | Matching pairs only | 9 tuples |
| emp ⟕ dept (left outer) | All emp tuples; unmatched padded with NULL | 10 tuples — Jose with dname, location = NULL |
| emp ⟖ dept (right outer) | All dept tuples; unmatched padded with NULL | 10 tuples — Operations with NULL employee attributes |
| emp ⟗ dept (full outer) | Unmatched tuples of both sides | 11 tuples — 9 matches + Jose + Operations |
| emp ⋉ dept (semi-join) | emp tuples that have a match, emp attributes only | 9 tuples |
For R(X, Y) and S(Y), the division R ÷ S returns the X-values that are paired in R with every Y-value in S. It answers queries containing 'all' or 'every': employees who work on all projects, students who passed every course. The result's attributes are those of R that are not in S.
The idea: build every pairing that should exist, subtract the ones that do exist, and anyone left owing a pairing is disqualified. Eby (105) is the instructive near miss — three of four projects, so one missing pair removes him.
-- 1. double NOT EXISTS: no project exists that the employee does not work on
SELECT e.ename
FROM emp e
WHERE NOT EXISTS (SELECT * FROM project p
WHERE NOT EXISTS (SELECT * FROM works_on w
WHERE w.emp_id = e.emp_id
AND w.proj_id = p.proj_id));
-- 2. counting: works on as many distinct projects as there are projects
SELECT emp_id
FROM works_on
GROUP BY emp_id
HAVING COUNT(DISTINCT proj_id) = (SELECT COUNT(*) FROM project);Practical algebra adds a few operators beyond Codd's set. Generalized projection allows expressions: π[ename, salary × 12 → annual](emp). Aggregation γ (Korth writes a calligraphic G, Navathe ℑ) groups and summarises: γ[dept_id; COUNT(emp_id), AVG(salary)](emp) gives (10, 2, 56000), (20, 4, 45000), (30, 3, 37666.67) and a group for the NULL dept_id. Assignment ← names an intermediate result: temp ← σ[dept_id = 20](emp).
Pure relational algebra works on sets; SQL works on bags (multisets) and keeps duplicates unless told otherwise. That is why SQL has both UNION (removes duplicates) and UNION ALL (keeps them). Pure algebra also cannot express recursive queries such as 'all employees under Anil at any depth' — SQL:1999 added WITH RECURSIVE for that.
| Query | Relational algebra | Result |
|---|---|---|
| Names of employees in Kochi departments | π[ename](emp ⋈ σ[location = 'Kochi'](dept)) | Bindu, Divya, Eby, Indu |
| Names of employees working on AI Lab | π[ename](emp ⋈ π[emp_id](works_on ⋈ σ[pname = 'AI Lab'](project))) | Bindu, Divya, Eby |
| Departments with no employees | π[dept_id](dept) − π[dept_id](emp) | {40} |
| Managers' subordinates: who reports to Bindu (102)? | π[ename](σ[manager_id = 102](emp)) | Divya, Eby |
| Highest salary without MAX | π[salary](emp) − π[e.salary](σ[e.salary < f.salary](ρ[e](emp) × ρ[f](emp))) | {90000} |
The 'highest salary' trick is worth understanding: the product pairs every salary with every other, the selection keeps salaries that are smaller than some other salary, and subtracting those from all salaries leaves only the maximum. The same pattern without aggregation appears in many MCQs.
Relational calculus is declarative: a query describes the tuples wanted using first-order logic, not the steps to compute them. In tuple relational calculus (TRC) variables range over tuples: {t | P(t)} is the set of tuples t for which formula P is true. Atoms are t ∈ R, t.A θ s.B and t.A θ c; formulas combine atoms with ∧, ∨, ¬, → and the quantifiers ∃ (there exists) and ∀ (for all). A variable is bound if a quantifier introduces it, otherwise free; only the result variable may be free.
In domain relational calculus (DRC) variables range over attribute values (domains), and a query has the form {⟨x₁, …, xₙ⟩ | P(x₁, …, xₙ)}. DRC is the basis of IBM's Query-By-Example (QBE), where users fill example values into skeleton tables; SQL draws mainly on TRC and the algebra.
A calculus expression is safe if it is guaranteed to return a finite result made only of values that appear in the database or the query (its domain). {t | ¬(t ∈ emp)} is unsafe: it asks for every possible tuple not in emp — infinitely many. Codd's theorem (1972) proves that relational algebra, safe TRC and safe DRC have exactly the same expressive power. A query language able to express every relational-algebra query is called relationally complete; SQL is relationally complete and more (aggregation, recursion, ordering).
| Operation (|R| = m, |S| = n) | Degree of result | Number of tuples |
|---|---|---|
| σ[c](R) | deg R | 0 … m |
| π[L](R) | |L| | 1 … m (non-empty R; fewer when duplicates collapse) |
| R ∪ S (union-compatible) | deg R | max(m, n) … m + n |
| R ∩ S | deg R | 0 … min(m, n) |
| R − S | deg R | 0 … m |
| R × S | deg R + deg S | exactly m·n |
| R ⋈ S (natural, k common attributes) | deg R + deg S − k | 0 … m·n |
| R ⟕ S | deg R + deg S − k | m … m·n (when n ≥ 1) |
| R(X, Y) ÷ S(Y) | deg R − deg S | 0 … ⌊m / n⌋ |
Two quick checks catch most mistakes. First, operators on rows (σ, −, ∩) never add attributes and never create tuples. Second, the only operator whose size is fixed regardless of data is ×. Division's upper bound follows because each answer value needs n distinct tuples in R.
Try the problem first, then reveal the working one step at a time.
Evaluate works_on[emp_id, proj_id] ÷ project[proj_id] on the sample data using only basic operators, and name the employee(s) found.
R(A, B, C) has 5 tuples and S(C, D) has 4 tuples; C is the primary key of S and R.C is a non-null foreign key referencing S. Find: (a) degree and cardinality of R × S; (b) degree and cardinality of R ⋈ S; (c) the maximum size of R ⋈ S if we knew nothing about keys; (d) whether R ∪ S is defined.
Write relational-algebra expressions and SQL for: (a) names of employees who work on both project 1 and project 2; (b) the highest salary without using MAX; (c) names of Research employees earning more than 40000.
Evaluate σ[job = 'Clerk' ∧ salary > 21000](emp) on the sample data. How many tuples and attributes does the result have?
R(A, B) = {(1, x), (1, y), (2, x), (3, x), (3, y), (4, y)} and S(B) = {x, y}. Compute R ÷ S, showing the intermediate relations.
Write a relational-algebra expression for 'departments that control no project' and evaluate it on the sample data.
R and S are union-compatible with |R| = 5 and |S| = 3. Give the minimum and maximum number of tuples of R ∪ S, R ∩ S, R − S and R × S.
Is {t | ¬(t ∈ emp)} a safe TRC expression? Then write a DRC query for the names of employees in department 30.
10 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.