Quick answer: Relational query languages come in two styles. Relational algebra is procedural: it says how to get the result, using operators such as selection σ, projection π, cross product ×, union ∪, difference −, rename ρ, and the derived join ⋈, intersection ∩ and division ÷. It always returns distinct tuples. Relational calculus (tuple and domain) is non-procedural: it says only what to retrieve. SQL is the practical language. SELECT-FROM-WHERE corresponds to π(σ(R₁ × … × Rₙ)), and SQL adds GROUP BY, HAVING, nested subqueries, IN, ANY, ALL and EXISTS. Safe TRC, safe DRC and basic relational algebra are equally expressive.
GATE asks you to evaluate relational algebra expressions and SQL queries on small tables, find result sizes, and spot equivalent queries. This chapter page maps the area and links to the detailed notes.
Chapter overview
| Topic | Key ideas | Detailed notes |
|---|---|---|
| Relational algebra | Basic and derived operators, joins, division, union compatibility, cardinality | Relational algebra |
| Tuple relational calculus | Syntax, quantifiers, safe expressions, equivalence to relational algebra | Tuple relational calculus |
| SQL | Execution order, GROUP BY and HAVING, nested and correlated queries, IN, ANY, ALL, EXISTS, NULLs | SQL queries |
Procedural vs non-procedural
| Procedural | Non-procedural |
|---|---|
| Says how to compute the result as well as what to retrieve | Says only what to retrieve |
| Relational algebra | Tuple and domain relational calculus |
Relational algebra in brief
| Operator | Symbol | Type |
|---|---|---|
| Selection | σ | Basic |
| Projection | π | Basic |
| Cross product | × | Basic |
| Union | ∪ | Basic |
| Set difference | − | Basic |
| Rename | ρ | Basic |
| Intersection | ∩, equal to R − (R − S) | Derived |
| Join | ⋈, a selection over × | Derived |
| Division | ÷, built from π, × and − | Derived |
Union, intersection and difference need union-compatible relations: the same number of attributes, with matching domains.
Cardinality bounds
R has n tuples and S has m tuples.
| Expression | Number of tuples |
|---|---|
| R × S | n × m |
| Natural join | 0 to n × m |
| Left outer join | n to n × m |
| Right outer join | m to n × m |
| Full outer join | max(n, m) to n × m |
| R ∪ S | max(n, m) to n + m |
| R ∩ S | 0 to min(n, m) |
| R − S | 0 to n |
| R ÷ S | 0 to ⌊n / m⌋ |
SQL in brief
- Written order: SELECT [DISTINCT] … FROM … WHERE … GROUP BY … HAVING … ORDER BY.
- Execution order: FROM → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY.
- SQL keeps duplicates unless you use DISTINCT, while relational algebra always removes them.
- WHERE filters rows; HAVING filters groups.
- For single-column comparisons, IN means = ANY and NOT IN means <> ALL.
- EXISTS checks whether a subquery returns any row.
Worked example
Question: Enroll(sid, cid) and Course(cid). Write a relational algebra expression for "students enrolled in every course".
Step 1: With division:
Step 2: With basic operators only:
The inner part finds (student, course) pairs that are missing. Subtracting those students from all enrolled students leaves the ones who took every course.
Summary table
| Language | Style | Duplicates | Typical GATE question |
|---|---|---|---|
| Relational algebra | Procedural | Removed (sets) | Output or size of an expression |
| Tuple / domain calculus | Declarative | Not applicable (sets) | Which expression matches an English query |
| SQL | Declarative | Kept unless DISTINCT | Output of a query on a given table |
Exam tips
- For relational algebra, think in sets and practise division.
- For SQL, trace the query in execution order and watch for NULLs and duplicates.
- "Every" or "all" in a question usually points to division, ∀ in calculus, or NOT EXISTS in SQL.
Common mistakes
- Forgetting that relational algebra removes duplicates while SQL does not.
- Applying set operations to relations that are not union compatible.
- Putting aggregate conditions in WHERE. They belong in HAVING.
- Treating NOT IN and NOT EXISTS as the same when NULLs can appear.
Practice questions
Q1. R has 4 tuples and S has 3. What are the maximum and minimum sizes of R ∪ S?
Answer: Maximum 7, minimum 4.
Q2. What does SELECT A FROM R WHERE P translate to in relational algebra?
Answer: , except that SQL keeps duplicates.
Q3. Can a HAVING condition use a column that is neither grouped nor aggregated?
Answer: No, not in standard SQL. HAVING conditions must use grouping columns or aggregate functions.
FAQs
What is the difference between relational algebra and relational calculus?
Relational algebra is procedural and describes a sequence of operations. Relational calculus is declarative and describes the properties of the tuples you want.
What is the division operator used for?
Queries that contain "all" or "every", such as students enrolled in every course.
Does SQL remove duplicate rows?
Not by default. SELECT DISTINCT removes them, and UNION, INTERSECT and EXCEPT remove them unless you add ALL.
What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY.
Are relational algebra and relational calculus equally powerful?
Yes. Basic relational algebra, safe tuple relational calculus and safe domain relational calculus can express the same queries.
Conclusion
Work through the detailed notes on relational algebra, tuple relational calculus and SQL queries. For schema design, see Normalization.