Re: NOT IN Query Returns No Results
From: Vishal Parkar (REMOVE_THIS_vgparkar_at_yahoo.co.in)
Date: 09/17/04
- Next message: Vishal Parkar: "Re: how to convert varchar into smalldatatime"
- Previous message: Baner: "NOT IN Query Returns No Results"
- In reply to: Baner: "NOT IN Query Returns No Results"
- Next in thread: Baner: "NOT IN Query Returns No Results"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 17 Sep 2004 22:01:27 +0530
use "not exists"
Internally in and exists clauses are evaluated differently. In clause is
internally evaluated as multiple OR conditions. Whereas EXISTS
clause checks for the subquery returns at least one row.
This is typical scenario when checking non-existing rows from the table.
select * from customers
where customerid not in(select customerid from orders union all select null)
If any of the rows from the subquery returns null value and if you are using
NOT IN clause then you will get any empty resultset.
You can use NOT EXISTS clause to get the required result set.
same query can be re-written as
select * from customers
where not exists
(select * from
(select distinct customerid from orders union all select null) a
where a.customerid = customers.customerid
)
-- Vishal Parkar vgparkar@yahoo.co.in | vgparkar@hotmail.com
- Next message: Vishal Parkar: "Re: how to convert varchar into smalldatatime"
- Previous message: Baner: "NOT IN Query Returns No Results"
- In reply to: Baner: "NOT IN Query Returns No Results"
- Next in thread: Baner: "NOT IN Query Returns No Results"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|