Oracle SQL: WHERE NOT EXISTS 无法正常运行

Oracle SQL: WHERE NOT EXITSTS not functioning properly

第一table

CREATE TABLE qualified (
    facultyid integer NOT NULL,
    courseid character varying(16) NOT NULL,
    datequalified character varying(16),
    dq2 date,
    CONSTRAINT qualified_pkey PRIMARY KEY (facultyid, courseid)
);

INSERT INTO qualified VALUES (3467, 'ISM 4212', to_date('9/1995', 'MM/YYYY'), date '1995-09-01');
INSERT INTO qualified VALUES (4756, 'ISM 3112', to_date('9/1991', 'MM/YYYY'), date '1991-09-01');
INSERT INTO qualified VALUES (2143, 'ISM 3113', to_date('9/1988', 'MM/YYYY'), date '1988-09-01');
INSERT INTO qualified VALUES (4756, 'ISM 3113', to_date('9/1991', 'MM/YYYY'), date '1991-09-01');
INSERT INTO qualified VALUES (2143, 'ISM 3112', to_date('9/1988', 'MM/YYYY'), date '1988-09-01');
INSERT INTO qualified VALUES (3467, 'ISM 4930', to_date('9/1996', 'MM/YYYY'), date '1996-09-01');

第 2 table:

CREATE TABLE faculty (
    facultyid integer NOT NULL,
    facultyname character varying(25),
    CONSTRAINT faculty_pkey PRIMARY KEY (facultyid)
);

INSERT INTO faculty VALUES (2143, 'Birkin');
INSERT INTO faculty VALUES (3467, 'Berndt');
INSERT INTO faculty VALUES (4756, 'Collins');

以下使用减号运算符的查询显示了正确的结果:

SELECT q.facultyid, facultyname
FROM qualified q, faculty fa
WHERE q.facultyid = fa.facultyid AND courseid = 'ISM 3113'
MINUS
SELECT q.facultyid, facultyname
FROM qualified q, faculty fa
WHERE q.facultyid = fa.facultyid AND courseid = 'ISM 4930';

以下 WHERE NOT EXISTS 显示不正确的结果

SELECT q.facultyid, facultyname
FROM qualified q, faculty fa
WHERE q.facultyid = fa.facultyid AND courseid = 'ISM 3113'
AND NOT EXISTS  (SELECT q.facultyid, facultyname
                   FROM qualified q, faculty fa
                   WHERE q.facultyid = fa.facultyid AND courseid = 'ISM 4930');

以下有效。 第二个 WHERE 子句必须具有引用第一个 FROM 子句中列出的 table 的 ALIAS,它拥有第一个 SELECT 子句中列出的所有列。

SELECT q.facultyid, facultyname
FROM qualified q, faculty fa
WHERE q.facultyid = fa.facultyid AND courseid = 'ISM 3113'
AND NOT EXISTS  (SELECT q.facultyid, facultyname
                   FROM qualified q2, faculty fa2
                   WHERE q2.facultyid = fa.facultyid AND courseid = 'ISM 4930');

更好的查询:

SELECT fa.facultyid, facultyname
FROM qualified q, faculty fa
WHERE q.facultyid = fa.facultyid AND courseid = 'ISM 3113'
AND NOT EXISTS  (SELECT facultyid
                   FROM qualified
                   WHERE facultyid = fa.facultyid AND courseid = 'ISM 4930');

感谢 'Joel Murach MySQL' 教科书提出了类似的问题。