Represent a subquery in relational algebra

AnEventHorizon picture AnEventHorizon · Oct 3, 2010 · Viewed 10.6k times · Source

How do I represent a subquery in relation algebra? Do I put the new select under the previous select condition?

SELECT number
FROM collection
WHERE number = (SELECT anotherNumber FROM anotherStack);

Answer

Martin Smith picture Martin Smith · Oct 5, 2010

You would just rewrite that as a join.

I'm not sure how widely used the syntax I learned for Relational Algebra is so in words.

  1. Take a projection of anotherNumber from anotherStack
  2. Rename anotherNumber from the result of step 1 as number
  3. Natural Join the result of step 2 onto collection
  4. Take a final projection of number from the result of step 3