1

tldr; Which of the three below can you not write in RA?

Our prof asked us to write 3 SQL queries. He then asked us which of them could not be written in Relation Algebra. They are as follows:

Given the following 3 tables:

Students (sID, sName);              (Primary Key sID)
Courses (cID, cName);               (Primary Key cID)
Enrolled (sID, cID, sectID, grade)  (Primary Key sID, cID)
where      foreign key (sID) references Students,  
           foreign key (cID) references Courses. 
  1. Find the largest student ID.
  2. Get a class list (consisting of 〈sID, sName〉) for COMP 4380, and arrange the records in the list in alphabetical order of sName. (He just wants all the students who have ever taken the course).
  3. Count the number of students enrolled in each course (identified by course ID).

So we have:

  1. A query involving finding the maximum in a column of integers.
  2. A query involving sorting the result in alphabetical order.
  3. A query involving counting multiple groups.

I've managed to write an RA statement for #1 (Details here) so I know that is not it. I'm not sure about 2 and 3 however. How could you sort with the RA operations? How could you count multiple groups? I would guess 3 is possible since RA can be extended to have count as an aggregate function.

Here are the SQL statements I came up with to answer my prof's original question:

SELECT max(sID) 
FROM Students

SELECT sID, sName
FROM Enrolled INNER JOIN Students
WHERE cID="COMP 4380"
ORDER BY sName

SELECT count(sID), cID
FROM Enrolled
GROUP BY cID

I haven't been able to write 2 and 3 in RA.

Community
  • 1
  • 1
Justin
  • 447
  • 4
  • 10
  • 33
  • Ref. http://cs.oberlin.edu/~jdonalds/311/lecture12.html Are you sure #2 *requires* sorting? –  Jan 30 '13 at 21:23
  • The article says we can do all of grouping, counting and sorting? – Justin Jan 30 '13 at 21:28
  • 1
    Not under *"standard"* RA: SQL is RA + Practical Extensions :D Sorting is *always* out of RA because it's no longer dealing with Sets which have no ordering. (Sorting is often used in the *implementation* of RA - i.e. merge joins in SQL.) –  Jan 30 '13 at 21:29
  • I see. You seem to be hinting that 2 can be done another way. But how can we order something properly without sorting :S – Justin Jan 30 '13 at 21:38
  • 1
    "He just wants all the students who have ever taken the course" doesn't imply sorting; I was just verifying that sorting *was* in the actual requirements. If so, that is *not* possible under RA. As far as *counting* groups: I don't know. –  Jan 30 '13 at 21:39

1 Answers1

0

Only 1 is possible in standard RA. We cannot sort, and we cannot count an arbitrary number in standard RA.

Justin
  • 447
  • 4
  • 10
  • 33