Database Management Systems (DBMS) Module 4
Relational algebra and relational calculus interview questions
Interview questions on relational algebra and relational calculus: warm-ups, then what each harder question tests and how a good answer starts.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
Why can π branch (Student) return fewer tuples than SELECT branch FROM student?
Answered in the lesson Relational algebra: the six core operators
-
Fresher
What does a natural join return when the two relations share no attribute names, and when they share all of them?
Answered in the lesson Joins in relational algebra and join sizes
-
Fresher
What does R ÷ S return when S is empty?
Answered in the lesson Division, grouping and extended relational algebra
-
Fresher
What makes a tuple relational calculus expression unsafe?
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Fresher to mid
A teammate says set difference is redundant in relational algebra, because a selection with NOT can filter tuples out. Show that − cannot be built from σ, π, ∪, × and ρ, and explain what a negated selection can and cannot do.
- What it tests
- Monotonicity of the other five operators, proof by a property every expression keeps, and the gap between a negated condition on one tuple and the absence of matching tuples.
- First step
- Ask what happens to each operator's result when an input relation gains a tuple: the results of σ, π, ∪, × and ρ can only gain tuples too, so every expression built from them is monotone. Then find a difference whose result shrinks when its second input grows.
- Follow-ups
-
- Is intersection monotone, and does it need difference?
- Why can σ with NOT not find the students with no enrolment?
- Which other queries of the sample college need difference?
From the lesson Relational algebra: the six core operators
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
Without aggregate functions or sorting, write a relational algebra expression for the courses with the second-highest number of credits in Course(cid, title, dept, credits). How large is the biggest intermediate result for n courses?
- What it tests
- Rename and self-products, the "not beaten by any other tuple" pattern applied twice, ties, and reasoning about the size of intermediate results.
- First step
- Find the maximum first: the courses that no other course beats, from a product of two renamed copies of Course and a difference. The courses that some course beats are the rest; applying the same pattern to the rest gives the courses with the second-highest value.
- Follow-ups
-
- What does your expression return when several courses tie for the highest value?
- How many tuples does Course × Course hold for 10,000 courses?
- How would you write it in SQL, and how does a database avoid the product?
From the lesson Relational algebra: the six core operators
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A report joins 2 million orders with customers and with their shipping addresses. After a release it returns 2.6 million rows. Using what keys guarantee about join sizes, how do you find the join that fanned out, and how do you fix the report?
- What it tests
- Join-size bounds from keys and foreign keys, testing uniqueness with GROUP BY, one-to-one against one-to-many joins, and fixes that keep one row per order.
- First step
- A join of orders with a table whose join column is unique keeps at most one row per order. Count the rows after each join in turn: the first step that exceeds 2 million joins a side whose join column is not unique. Confirm it with GROUP BY on that column HAVING count(*) > 1.
- Follow-ups
-
- The address table now keeps a row for every change of address. What should the report join?
- Would a foreign key constraint have prevented this?
- Why is a DISTINCT on the final result not a safe fix?
From the lesson Joins in relational algebra and join sizes
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
List every course with the number of CSE students enrolled in it, including courses with none. Write it in SQL and in extended relational algebra, and name the two places where a first attempt usually loses or miscounts the zero rows.
- What it tests
- Outer joins and where their conditions go, COUNT(*) against COUNT(column) on padded rows, and the extended algebra's outer join with grouping.
- First step
- Start from course and keep every row with LEFT JOIN. Only enrolments of CSE students may count, so the branch test belongs in the ON clause of the join chain, not in WHERE. Then count a column of the student side, which is NULL in every row that does not count.
- Follow-ups
-
- What does COUNT(*) return for CS301 and for MA101, and why?
- How can you write the same query without an outer join?
- What changes if a course appears twice in the course table?
From the lesson Joins in relational algebra and join sizes
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Find the students who have taken every course offered by the department named like their branch: a CSE student needs every CSE course. Why is this not a single R ÷ S, and how do you write it in relational algebra and in SQL?
- What it tests
- Division whose divisor depends on each candidate, rewriting "every" as "not missing any", correlated NOT EXISTS, and the vacuous case of a branch with no courses.
- First step
- R ÷ S divides every candidate by the same S, but here each student's divisor is a different set, the courses of their branch. Build the pairs each student should have, Student joined with Course on branch = dept, subtract the enrolments, and remove every student who appears among the missing pairs.
- Follow-ups
-
- What should happen to a student whose branch offers no courses, such as ME?
- How many pairs does the relation of required pairs hold on the sample college?
- How would a version that counts courses have to change?
From the lesson Division, grouping and extended relational algebra
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
With each student's credit-weighted grade points computed as in extended.sql, list for each branch the students with the highest value, ties included. Write it in extended relational algebra and in SQL without window functions, and say what SELECT branch, name, max(points) with GROUP BY branch gets wrong.
- What it tests
- Composing grouping steps, joining an aggregate back to keep ties, students without grades, integer division, and the bare-column problem of non-grouped columns.
- First step
- Work in three steps. Group the enrolments by student to get each student's points and join Student for the branch. Group those by branch with max to get one best value per branch. Then join the per-student values back on branch and points equal to the best value, which keeps every student who ties.
- Follow-ups
-
- Which students does it return on the sample college?
- What happens to Farhan, who has no grades?
- How would a window function do it, and which ranking function keeps ties?
From the lesson Division, grouping and extended relational algebra
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Write a tuple relational calculus query for the pairs of different students who are enrolled in exactly the same set of courses, then translate it into SQL. What does your query say about two students who have no enrolments at all?
- What it tests
- Set equality as two universal conditions, implication inside ∀, translating each ∀ into NOT EXISTS, and vacuous truth for empty sets.
- First step
- Two sets are equal when each contains the other. Take two student variables s and t with s.sid < t.sid, and require that every enrolment of s has an enrolment of t in the same course, and every enrolment of t one of s. Each "every" becomes a NOT EXISTS around a NOT EXISTS in SQL.
- Follow-ups
-
- Which pairs does it return on the sample college?
- Why s.sid < t.sid rather than s.sid ≠ t.sid?
- How would you test set equality by counting, and when does that break?
From the lesson Tuple and domain relational calculus
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
"Students who passed every course they took" is ∀e ∈ Enrolled (e.sid = s.sid ⇒ passed(e)), and some grades are still NULL. Compare NOT EXISTS (... AND e.grade = 'F') with NOT EXISTS (... AND NOT (e.grade <> 'F')), say whom each wrongly includes, and write a correct query.
- What it tests
- Translating ∀ and ⇒ into NOT EXISTS under three-valued logic, deciding what NULL means for the rule, and vacuous truth for students with no enrolments.
- First step
- Decide first what an ungraded course means for the rule: not passed yet. Then write ∀ as "no enrolment of the student exists that is not passed", and spell out "not passed" so that it is true for NULL: grade = 'F' OR grade IS NULL. Test every version on a student whose only grade is NULL.
- Follow-ups
-
- What does each version return for a student with no enrolments?
- How would the schema remove the ambiguity?
- How does the same NULL problem show up with NOT IN?
From the lesson Tuple and domain relational calculus
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
All Database Management Systems (DBMS) interview questions · The Database Management Systems (DBMS) track