Can you please see if the following query is correct? If not, could you provide the right syntax for a subquery which contains the "not in" condition?
select bid from bp where bid not in (select bid from bp_hist)l
Requires Free Membership to View
When you register, my team of editors will also send you resources covering Linux administration and management; integration and interoperability between Linux, Windows and Unix; securing Linux and mixed-platform environments; and migrating to Linux.
Margie Semilof, Editorial DirectorLet us first create table bp and insert a few rows into it:
(1 row affected)
(1 row affected)
(1 row affected)
(1 row affected)
Now create a historical table bp_hist:
(3 rows affected)
Now try the first syntax:
- select bid from bp where bid not in (select bid from bp_hist)
- go
bid
-----------
4
As you can see, the syntax is correct. However, a more efficient way of doing this query is to use the NOT EXISTS clause in the query as shown below:
- select bid from bp a where not exists(select 1 from bp_hist b where a.bid = b.bid)
- go
bid
-----------
4
This was first published in December 2005