Introduction
Relational algebra is a procedural query language: a query is an expression built from operators that take relations and return a relation. Because the output of every operator is again a relation, operators compose freely, and an expression reads like a pipeline. Every SQL query is translated into an algebra expression before it is optimised and executed, so the algebra is also the vocabulary of query processing.
The algebra uses set semantics: relations have no duplicate tuples, and every operator removes duplicates from its result. This single fact explains several traps, such as the cardinality of a projection. What an expression returns on given tables, how many tuples a join can produce, and which expression expresses a "for all" query (division) are the standard calculations.
Prerequisites
- Relational Model and Keys: relations as sets, keys and foreign keys.
- Sets: union, intersection, difference and Cartesian product.
Core concepts
The six basic operators
| Operator | Notation | Result |
|---|---|---|
| Selection | tuples of satisfying predicate | |
| Projection | the listed columns, duplicates removed | |
| Union | tuples in either; , union-compatible | |
| Set difference | tuples in and not in ; union-compatible | |
| Cartesian product | every pairing of a tuple of with a tuple of | |
| Rename | same tuples, new relation and attribute names |
Union compatibility. and have the same arity and corresponding attributes have the same domains. Names may differ; the result takes the names of .
These six are the fundamental operators: every derived operator can be written with them, and together they make a language relationally complete.
Derived operators
| Operator | Definition with basic operators |
|---|---|
| Intersection | |
| Theta join | |
| Equijoin | theta join whose is a conjunction of equalities |
| Natural join | equality on all common attribute names, then one copy of each common attribute kept |
| Division , , | |
| Assignment | names an intermediate result |
Outer joins
A natural or theta join drops a tuple that has no partner (a dangling tuple). Outer joins keep them, padding the missing attributes with NULL. The three outer joins are written as (left), (right) and (full): the bracket marks the side whose dangling tuples are kept. Textbooks draw the same operators as a bowtie with extended horizontal strokes on the kept side.
- Left outer join : all of plus each dangling tuple of padded with NULLs.
- Right outer join : plus each dangling tuple of .
- Full outer join : plus dangling tuples of both.
In an ordinary equality/natural join, NULL equality is not TRUE, so NULL join keys do not match even each other. This does not apply to every theta predicate: IS NULL, null-safe equality or a TRUE OR branch can permit a match; evaluate the specified predicate.
How it works: the joins on one pair of tables
Algorithms and pseudocode
Division answers "which are related to every in ". The textbook expression works in three steps:
DIVIDE(R(X, Y), S(Y))
T1 <- pi_X(R) // every candidate x
T2 <- (T1 x S) - R // pairs (x, y) that WOULD be needed but are missing
T3 <- pi_X(T2) // x values missing at least one y
return T1 - T3A direct evaluation sorts or hashes by and counts, for each , how many distinct of it has; qualifies when the count equals . After hashing for expected membership tests, sorting gives expected time; sorting both inputs requires accounting for both sorting costs.
Nested-loop evaluation of costs predicate tests; hash join on an equality costs on average.
Mathematical explanation
Cardinality bounds for , , both non-empty:
| Expression | Minimum | Maximum |
|---|---|---|
| 0 | ||
| 1 | ||
| 0 | ||
| 0 | ||
| 0 | ||
| , common attributes a key of | 0 | |
| 0 |
Join with a foreign key. If the common attributes of and form a foreign key of , declared NOT NULL and referencing the primary key of , then every tuple of matches exactly one tuple of and .
Where the outer-join bounds come from. Write , and for the numbers of dangling tuples. Then .
- Maximum: either everything matches everything (, nothing dangles) or nothing matches (, all tuples dangle). The larger wins, so for the maximum is 2, not 1.
- Minimum: every tuple of either side appears in at least one output tuple, so the result has at least tuples; this is reached when each tuple of the smaller side matches exactly one tuple of the larger and the rest of the larger side dangles. For two 4-tuple relations, as in Figure 1, the full outer join has between 4 and 16 tuples; Figure 1 has 7.
- If the common attributes are a key of , each tuple matches at most one tuple, so the inner join has at most tuples; the minimum 0 needs only that no value matches.
Natural join special cases. With no common attributes, . With identical schemas, . Always .
Why the division formula is right. is in the result iff for every . lists every pair that would be required; subtracting leaves exactly the required-but-missing pairs; their values are the ones that fail, and removing them from the candidates leaves the answer. If is empty, nothing is required, nothing fails, and .
Worked examples
Example 1: division. Enrolled(sid, cid) = .
- Divided by
Req(cid)= : . Here , so . - Divided by : .
- Divided by the empty relation: .

Example 2: maximum without aggregation. For , the largest value is
The subtracted part is every value smaller than some other value, , so the result is .
Example 3: "at least two courses". on the Enrolled relation of Example 1 returns all four students; "exactly one course" would be minus that result, which is empty here.
Example 4: projection then selection order. on the of Figure 1, but is not even well formed: was projected away. Selection and projection commute only when the projection keeps every attribute that the predicate uses.
Example 5: join with a foreign key. EMP(eno, dno) with 5 rows and dno a NOT NULL foreign key to DEPT(dno, dname) with 3 rows: whatever the data. If 2 employees had dno NULL instead, the natural join would have 3 tuples and the left outer join 5.
Important properties and observations
- Set semantics. Projection and union remove duplicates; can be less than unless contains a key.
- Selections cascade and commute: .
- Projections cascade: when .
- Selection distributes over , and ; projection distributes over but not over or . Example: , : but .
- Natural inner joins are commutative and associative (up to attribute order); theta joins require correctly reassociated predicates, and outer joins are excluded; and are the expensive operators, so optimisers push selections below them.
- Difference is not commutative; and differ in general.
- Mixing inner and outer joins is not associative: can differ from . A selection on an attribute of the padded side also cannot be pushed below an outer join: on Figure 1 has 3 tuples, has 4.
Related material. Aggregation and grouping , duplicate elimination and sorting belong to the extended algebra used for SQL; the GATE syllabus algebra is the set-based core above. Semi-join appears in distributed query processing.
Common misconceptions.
- "." No: up to , when every tuple matches every tuple.
- " is empty when is empty." No: it is .
- "Natural join compares NULLs as equal." It does not; NULL never satisfies an equality.
GATE examination perspective
- Evaluate an expression. Work on small tables: track duplicates removed by projection and NULL padding of outer joins.
- Cardinality questions. Minimum and maximum tuples of a join, union or difference; joins on a foreign key.
- "For all" queries. These are division; recognise the shape even when written without .
- Equivalence of expressions. Which rewrites (pushing selection, distributing projection) are valid.
- Self-joins with rename. Apply this to "at least two", "maximum" and "pairs" queries.
Summary
Sources
- A. Silberschatz, H. F. Korth, S. Sudarshan, Database System Concepts, 7th ed., McGraw-Hill, 2019 (2020 copyright), 2.6 The Relational Algebra; Chapter 27 Formal Relational Query Languages.
- R. Elmasri, S. B. Navathe, Fundamentals of Database Systems, 7th ed., Pearson, 8.1 Unary Relational Operations: SELECT and PROJECT; 8.2 Relational Algebra Operations from Set Theory; 8.3 Binary Relational Operations: JOIN and DIVISION; 8.4 Additional Relational Operations.
