The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+20 votes

Consider the following relational database schemes:

  • PRE-REQ(Cno, pre-Cno)
  • COMPLETED (student_no, Cno)

COURSES gives the number and name of all the available courses.

PRE-REQ gives the information about which courses are pre-requisites for a given course.

COMPLETED indicates what courses have been completed by students

Express the following using relational algebra:

List all the courses for which a student with student_no 2310 has completed all the pre-requisites.

asked in Databases by Veteran (52k points)
edited by | 2k views

In relation COURSES  ,(comma) missing

 COURSES ( -> COURSES (Cno. , name)

see detail explanation at the last of this page...

check queries on this tool, it's awesome.
$\text{COMPLETED-COURSES} \leftarrow \rho \ (\sigma_{(student\_no = 2310)} \text{COMPLETED})$


$\pi_{\text{PRE-REQ.Cno}} (\text{COMPLETED-COURSES}\ \boxtimes \ _{\text{(COMPLETED-COURSES.Cno} = \text{PRE-REQ.pre-Cno)}} \text{PRE-REQ}) $


Is this relational algebra expression correct?

13 Answers

+11 votes
Best answer
$S \leftarrow \pi_{Cno}(\sigma_{student\_no=2310}(COMPLETED))\\RESULT \leftarrow ((\rho_{(Course,Cno)}(PRE-REQ)) \div S )$
answered by Boss (17.4k points)
selected by

we will select CNO for stud_no=2310 and rename CNO as pre-cno in COMPLETED relation and then perform divison of PreReq table with the table created above. Right??
No, it won't work. Say if student completed two courses C1 and C2 that are prerequisite for C3 and C4 respectively. Then what will ur query return?. It will return Null table.
Bcoz neither C3 has both prerequisite as C1 and C2 nor C4 has both prerequisite.
so can u write the correct query in relational algebra as sql both??


The division operation p(P) ÷ q(Q) only makes sense when Q ⊆ P.

In the above assignment, there are no common attributes between the two relations as the only common attribute 'Cno' has been renamed to 'Course', so the division makes no sense.

Your answer is incorrect because it is not even a valid expression.

Can I do this

Project courseno (Select studentno=2310 (completed join pre requisite))

Will this work for the below database?


Cno Cname
c1 A
c2 B
c3 C
c4 D
c5 E


Cno Pre-Cno
c1 c3
c2 c1
c2 c4
c5 c2
c5 c3

In graphical form:


Which shows that c3 and c4 do not need any pre-course and rest others do. There shouldn't be any cycle in this graph otherwise no course can ever be completed. Like if pre-course for c1 is c2 and pre-course for c2 is c1 then we cannot make any progress.

Sno Cno
s1 c3
s1 c4
s2 c1
s3 c2

Now if I want to know what are the courses that s1 can complete after having taken the pre-courses, then:

pre courses taken by s1 are c3 and c4.

With the knowledge of c3 and c4 only c1 can be completed. 

After taking c1, s1 can then take c2 and then c5. But these shouldn't be the current result.

According to the query:

S will have {c3,c4} and when we divide pre-req with it, the result we get is null. Isn't it? If so then this is not giving correct result. 

@Sachin I saw the pdf you attached.. But there you haven't considered the case if a student has completed more than one course. 

Please help :/



R←πCnoCno X S

M←πCno(Pre-Req - R)

πCno(πCno(Cno) - (M∪S) )

I am getting this.

For S1,


R= <c1,c3>, <c1,c4>,<c2,c3>,<c2,c4>,<c3,c3>,<c3,c4>,<c4,c3>,<c4,c4>,<c5,c3>,<c5,c4>

M= πCno(<c2,c1>,<c5,c2>) = {c2,c5}

πCno(Cno - (M∪S) )= πCno({c1,c2,c3,c4,c5} - ( {c2,c5}∪{c3,c4}) ) = c1

For S2, answer should be c3 and c4 only because they don't need any pre-course.

Please correct me if wrong.


 yes i also think in the same way.  answer seem to be same as yours.

+7 votes

SQL query will be 

FROM Completed, Pre-Req  
WHERE student_no = '2310'  
GROUP BY cno  
HAVING pre-Cno IN (
    SELECT C.cno 
    FROM Completed AS C
    WHERE C.student_no = '2310';
answered by Boss (30.6k points)
But question is for relational algebra query.
arjun sir can u plz write RELATIONAL ALGEBRA for it?? i am not able to figure out. :-(

The one given by @mint below looks right to me. Verify
ΠCno.(PRE-REQ ⋈pre Cno.=Cno.   ΠCno.Sno.=2310(COMPLETED)))

Is it right sql query for above scenario???

select COURSES.cno

@amarVashishth I think your sql query won't work because "IN" operator is a shorthand for multiple "OR" conditions in SQL so here any course no will be displayed for which any one of the pre-req course has been completed But here we need For All condition.

FROM Courses C1
                                       FROM PRE-REQ P1
                                       WHERE C1.Cno=P1.Cno
                                       SELECT Cno
                                       FROM COMPLETED C2
                                       WHERE C2.student_no='2310' ) ) ;

+4 votes

RA query: ΠCno.(PRE-REQ ⋈pre Cno.=Cno.   ΠCno.Sno.=2310(COMPLETED)))   

make it in two parts:

X= ΠCno.Sno.=2310(COMPLETED))      {X gives all the tuples in which 2310 is present}

YCno.(PRE-REQ pre Cno.=Cno.   X)   

I think Y is the required answer... correct me if i wrong.

answered by Active (1.1k points)
I got the same answer. I am wondering my no one commented here before. Veterans ?

PRE-REQ gives the information about which courses are pre-requisites for a given course, hence a course may have many prerequisite courses. In that case suppose a course C1 has two prerequisite courses- CP1 and CP2, now even if Sno-2310 completes only one of those prerequisite courses then also above query will print C1, which it should not, if and only if Sno-2310 completes both CP1 and CP2 then only it should print C1

There maybe more than one prereq for a single course.
+2 votes

T1 will have all the available course numbers

T2 will have all the course numbers completed by student2310

T3  will have the combination of all the courses and the courses completed by student2310

PRE_REQ - T3 (set minus operation) will return us all the entries of PRE_REQ which are not there at T3,

Suppose <C1,C5> is a particular tuple of (PRE-REQ - T3),

Now what does it implies? -->It implies that C5 is one of the prerequisite course for C1 which has not been completed by C5. Proof: If student2310 would have completed C5 then definitely <C1,C5> should have been there at  T3(remember T3 is the combination of all the courses and the courses completed by student2310) and in that case (PRE_REQ - T3) can't have  <C1,C5> as a tuple.

So, for any such <C1,C5> tuple, (<C1, any  course id>) of PRE_REQ - T3, C1  should not be printed as output(Since there is some prerequisite course for C1 which student2310 has not completed).

Now, suppose we have not got any tuple as a result of (PRE_REQ - T3) where C2 is there under cno attribute(<C2, any course id>), what does it implies?--> It implies that student2310 has completed all the prerequisite courses C2.

Hence, in order to get the final result we need to project cno from (PRE_REQ - T3) and subtract it from T1.

answered by Active (2.4k points)
this is the correct answer. Rest all above give wrong answer.
+1 vote

FROM Courses C1


                                      FROM PRE-REQ P1

                                      WHERE C1.Cno=P1.Cno


                                      SELECT Cno

                                      FROM COMPLETED C2

                                      WHERE C2.student_no='2310' ) ) ;

Is this query correct ?
answered by (51 points)
0 votes

This should be the answer.

ΠCno(PRE-REQ) - ΠCno(PRE-REQ - ρPRE-REQ.Cno/CnoPRE-REQ.Cno,pre-Cno((σstudent_no=2310 (COMPLETED))⋈Cno=pre-Cno PRE-REQ)))

answered by Active (1.7k points)
edited by
0 votes
My Attempt

$\large \pi_{\text{NAME}}(\text{COURSES}\ * \ \text{PREREQ}\ \div \rho_{\text{CNO=PRECNO}}(\text{COMPLETED}) )$
answered by Junior (779 points)
0 votes

MY attempt please let me know where i m wrong

answered by (67 points)
edited by
0 votes

X= ρ(pre-Cno)Cno.Sno.=2310(COMPLETED)))  

YCno.(PRE-REQ   X)  

Y is  the correct answer.
correct me if I am wrong

answered by (61 points)
0 votes

I came up with two approach for this Question.

Note- Questions in which "ALL" keyword is used, most probably would be solved with division operation.  

answered by Active (1.8k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,541 questions
54,071 answers
70,978 users