Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5725165
  • 博文数量: 745
  • 博客积分: 10075
  • 博客等级: 上将
  • 技术积分: 7716
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-29 12:09
文章分类

全部博文(745)

文章存档

2019年(1)

2016年(1)

2010年(31)

2009年(88)

2008年(129)

2007年(155)

2006年(197)

2005年(143)

分类: Oracle

2006-12-27 16:17:43

Null Values in a subquery
SELECT emp.last_name
FROM   employees emp
WHERE  emp.employee_id NOT IN
                              (SELECT  mgr.manager_id
                               FROM    employees mgr);
no rows selected
The SQL statement in the slide attempts to display all the employees who do not have any subordinates. Logically, this SQL statement should have returned 12 rows. However, the SQL statement does not return any lows. One of the values returned by the inner query is a null value, and hence the entire query teturns no rows. The reason is that all conditions that compare a null value result in a null. So whenever null values are likely to be part of the results set of a subquery, do not use the NOT IN operator. The NOT IN operator is equivalent to <> ALL.
Notice that the null value as part of the results set of a subquery is not a problem if you use the IN operator. The IN operator is equivalent to =ANY. For example, to display the employees who have subordinates, use the following SQL statement:
SELECT emp.last_name
FROM   employees emp
WHERE  emp.employee_id IN
                          (SELECT  mgr.manager_id
                           FROM    employees mgr);
Alternatively, a WHERE clause can be included in the subquery to display all the employees who do not have any subordinates:
SELECT last_name FROM  employees
WHERE  employee_id NOT IN
                          (SELECT  manager_id FROM  employees
                           WHERE   manager_id IS NOT NULL);

 
阅读(1984) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~