在做课本上的一道SQL题,搞不明白,请大家帮我分析下,答案无所谓,关键是让我明白这两条语句有什么区别?
谢谢大家~好人有好报哦~
问题如下:
Queries often return unexpected results when NULL values are present.
Let's consider the scenario of our 'University' schema from the lecture slides and the labs 
that we extend as follows (we assume that you have the schema 'UniDB' in your search-path;
otherwise adde the prefix 'unidb.' in front of all table names): 
 ALTER TABLE AcademicStaff ADD COLUMN mainUnit CHAR(8) REFERENCES UnitOfStudy; 
Now consider the following two queries which try to identify those units that are not ’main’ unit of studies by any lecturer:
Query Q1     SELECT * 
       FROM UnitOfStudy 
      WHERE uosCode NOT IN (  
               SELECT mainUnit 
                 FROM AcademicStaff 
                WHERE deptID = 'SIT' )  
Query Q2     SELECT * 
       FROM UnitOfStudy u 
      WHERE  NOT EXISTS ( 
              SELECT * 
                FROM AcademicStaff 
               WHERE deptID='SIT' AND mainUnit=u.uosCode ) 
If there is a new SIT lecturer who has not specified his main unit-of-study yet
(the corresponding attribute is hence NULL), these two queries give different results. 
How and Why??选项如下A. Only query Q1 returns the correct result of all units of studies which have not been nominated as 'mainUnit' by any lecturer yet.
The reason is that the sub-query of query Q2 will not find any unit if a lecturer has a 'mainUnit' of NULL as the corresponding comparison 'mainUnit = u.uosCode' will then be UNKNOWN.B. Both query Q2 and query Q1 will not return anything (empty results) if there is at least one lecturer with an unknown (NULL) 'mainUnit'. The reason is that any comparison with a NULL value is UNKNOWN, and as in both sub-queries of query Q1 and of query Q2, there are such comparisons, no unit of study will qualify for the result.C. Only query Q2 returns the correct result of all units of studies which have not been nominated as 'mainUnit' by any lecturer yet.
The reason is that the sub-query of query Q1 returns a NULL value for each lecturer who hasn't set 'mainUnit' yet, and that any comparison involving a NULL value yields UNKNOWN. So the comparison with 'NOT IN' is never true if there is at least one NULL returned by the sub query, and hence Q1 returns an empty result.D. Only query Q1 returns the correct result of all units of studies which have not been nominated as 'mainUnit' by any lecturer yet.
The reason is that this query Q1 is using the 'NOT IN' comparison which can deal with the special case that some if the 'mainUnits' selected from AcademicStaff are NULL, while the NOT EXISTS cannot find any of these cases答案无所谓,关键是让我明白这两条语句有什么区别?SQLNULL

解决方案 »

  1.   

    select * from tt where id in (select null from dual union select 1 from dual);
    相当于
    select * from tt where id is null or id=1select * from tt where id not in (select 1 from dual union select null from dual);
    相当于
    select * from tt where id is not null and id!=1
      

  2.   

    上面那个不准确,你看看这个
    select * from tt where id in (select null from dual union select 1 from dual);
    相当于
    select * from tt where id = null or id=1select * from tt where id not in (select 1 from dual union select null from dual);
    相当于
    select * from tt where id <> null and id!=1
      

  3.   

    这个应该是这样理解吧:
    如果AcademicStaff 表中 有一条记录deptID = 'SIT',mainUnit = null。这个时候not in语句跑出来的结果将会是空的。而not exists结果会是正确的。