Re: Examination

From: Damjan S. Vujnovic <>
Date: Tue, 11 Feb 2003 10:46:10 +0100
Message-ID: <b2agmg$k49$>

"Clairwil" <> wrote:

> I'd like to know if somebody can tell me how to do this:
> I have such a db:
> Student (IdStudent, Name, Surname, Address)
> DegreeThesis (IdThesis, Ttle, IdTeacher, IdStudent, Mark, Year)
> Teacher (IdTeacher, Surname)
> What I'm asked to find is
> all the IdThesis that have got the best mark for each year for each
> who's now linked to (at least) two theses not yet evaluated (it means mark
> =0).
> I can find rightly all the teacher and all best marks, but I can't link
> correctly to their theses: it selects IdThesis related to a correct
> also if the mark is not the best of that year, but it is present in the sq
> Mark IN.
> This is my SQL code:
> (SELECT IdThesis, IdTeacher, Mark, Year FROM DegreeThesis
> WHERE IdTeacher IN
> (SELECT IdTeacher FROM
> (SELECT IdTeacher , COUNT(*) FROM DegreeThesis WHERE Mark= 0
> GROUP BY IdTeacher HAVING COUNT(*)>=2)
> )
> AND Mark IN
> (SELECT MaxMark FROM (SELECT IdTeacher , Max(Mark) AS MaxMark
> FROM DegreeThesis WHERE Mark<> 0 GROUP BY IdTeacher , Year )
> );

I'm not sure that I got the point, but try this (not tested):

SELECT D1.IdThesis
  FROM DegreeThesis D1
 WHERE D1.Mark = (

       SELECT MAX(D2.Mark)
         FROM DegreeThesis D2
        WHERE D2.Year = D1.Year)
   AND D1.IdTeacher IN (
       SELECT IdTeacher
         FROM DegreeThesis
        WHERE Mark=0
        GROUP BY IdTeacher
       HAVING Count(*) >= 2)

As an alternative, you can replace IN with EXISTS and correlated (sub)query. And, btw, your original code is very "interesting"...

Damjan S. Vujnovic

University of Belgrade
School of Electrical Engineering
Department of Computer Engineering & Informatics Belgrade, Yugoslavia Received on Tue Feb 11 2003 - 10:46:10 CET

Original text of this message