Using Relational Algebra to Select Based on Query Results

The title of this post is a bit misleading, but it just goes to show how important it is to ask the right question. The other day, I was working on a challenge homework problem that basically proposed this challenge:

Use relational algebra to find all schools that offer every class Spanish majors want to take.

(The actual problem was different, but I don’t want future students to find the answer just by searching for the homework question!) Assume we have two relations:

  • Relation S: A list of schools and the classes they offer.
  • Relation C: A list of students, their majors, and the classes they want to take.

At first, all I could imagine was that I needed to find every class Spanish majors wanted to take, and then use the results of that query to iterate over the list of schools. I couldn’t think of a different way to approach the problem … until I thought back to the approach I used in the maximum value problem I discussed yesterday. Continue reading “Using Relational Algebra to Select Based on Query Results”

Finding a Maximum Value with Relational Algebra

I finally coded the correct answer to my maximum value problem in relational algebra. There are plenty of answers out there (see stack overflowcodeblow, stack overflow again, …) but my brain didn’t want to accept that this solution worked. It helps me to visualize what’s happening.

Say we have this simple relation of letters and numbers:

Letter Number
A 1
B 2
C 3
D 4

Continue reading “Finding a Maximum Value with Relational Algebra”

Tackling JSON and Relational Algebra

My Intro to Databases course moved on from XML to JSON, which I have to admit was less motivating for me. JSON has an interesting format, but I don’t really know what I would use it for. In any case, we didn’t spend much time on it. I zipped through that section and onto a more hefty topic: relational algebra.

I had a somewhat satisfying moment when my husband, an electrical engineer, offered to help me with a homework problem I was stuck on. (He is my go-to guy for helping me learn or relearn the more complex math and logic problems I encounter in my computer science courses.) I started to explain the homework problem, when he stopped me: “Wait, what is relational algebra?” I explained the general idea and the allowed operations, and he admitted, “Huh, I have never done that before.” As he wandered back to his own work, I turned back to my desk with even more resolve. A technical concept I could master that he doesn’t already know? I’ll do it! Continue reading “Tackling JSON and Relational Algebra”