0

Five tables:

//Applicant
A#
-----------------
1
2
3
4
5

//coursepass
A#         ctitle
-------------------
1          hello
2          testing
3          yeah
4          coding
5          computer

//position
p#
----------------
0001
0002
0003
0004
0005
0006

//applies
A#         p#
-------------
1          0001
2          0001
3          0002
4          0003
5          0004
6          0005

//Sneeded
p#         sname
-----------------
0001        Java
0002         C++
0003         C#
0004         C
0005         C

My question is to find the titles of coursepassed by the applicant who did not apply any position that need a skill name ='C'

So for my table above. we can see that p# 0004,0005 need 'C', so the A#=1,2,3,4 apply an position but the position doesn't need 'C' so refer back to the coursepass, the ctitle hello, testing, yeah, coding will be display

//RESULT
CTITLE
-------------
hello
testing
yeah
coding

I use this query, but cannot get the result I want

SELECT cp.CTITLE
  FROM COURSEPASSED cp
 WHERE NOT EXISTS (SELECT a.A#
                     FROM APPLIES a
                     JOIN SNEEDED sn ON a.P#=sn.P# and sn.SNAME='C');

The following query returns the result I expect.

SELECT cp.CTITLE 
  FROM COURSEPASSED cp
 WHERE cp.A# NOT IN (SELECT a.A#
                       FROM APPLIES a
                       JOIN SNEEDED sn ON a.P#=sn.P# and sn.SNAME='C');

But I want to use NOT EXISTS condition. How should I change my query?

1
  • i just typo . but it's still fail to get the result Commented May 15, 2014 at 16:17

1 Answer 1

2

Your NOT EXISTS clause should reference some value from the outer table.

SELECT cp.CTITLE FROM COURSEPASS cp
WHERE NOT EXISTS
(
    SELECT a.A# FROM APPLIES a JOIN SNEEDED sn 
    ON a.P#=sn.P# and sn.SNAME='C'
    WHERE a.A# = cp.A#    // notice reference back to cp
);
Sign up to request clarification or add additional context in comments.

2 Comments

after your explanation, i able to do it already ! THANKS YOU !
by the way can you help me to see this thread? stackoverflow.com/questions/23674644/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.