0

I was asked to do a query in sql using double negation. The question itself is asking for all the "sigla" and "disciplina "where the semestre_id is 21 and has at least 1 attribute "resposta"=5

table http://i.imgur.com/8CDoFHZ.png

query http://i.imgur.com/5aueUKS.png

Now despite posting all this my question is mostly that I am not too sure if this is the way of doing a proper double negation in sql, since I am getting as an answer all the lines of the table which is wrong. Since I am having an hard time searching for examples online could anyone clarify me?

Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
user697110
  • 589
  • 10
  • 25
  • you want to _where the semestre_id is 21_ but you selecting where semestre_id is not 21 – Justin Mar 28 '13 at 12:43
  • I thought I was first selecting those who had resposta 5 then selecting the semestre 21 that don't have resposta 5 and then selecting those that aren't semestre 21 that don't have resposta 5 – user697110 Mar 28 '13 at 12:59
  • your question states AND for the condition. Your comment expresses an exclusive OR. Are you sure of your translation to english? – Sebas Mar 28 '13 at 13:51
  • My bad I meant " that are semestre 21 that don't have resposta 5" which then the last "not in" would exclude, leaving only those that have resposta 5 – user697110 Mar 28 '13 at 14:09

1 Answers1

1
select  disc.disciplina_id, disc.sigla
from    ipdw_disciplina disc
        inner join ipdw_respostas resp
            on disc.disciplina_id = resp.disciplina_id
where   resp.semestre_id = 21
        and resp.resposta = 5
group by disc.disciplina_id, disc.sigla

i try to avoid in / not in whenever possible. It seems easier to follow the intent of the query without them. This looks like a pretty straight forward query that does not need the double negation.

jhinkley
  • 668
  • 4
  • 10
  • Yes without double negation it would be easier, however the questions explicitly states that I must use double negation. – user697110 Mar 28 '13 at 13:51