473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Strange results from "not in" query

Hi all,

Using SQL Server 2000, SP4.

I have a table of street names (Rua) whose ids (cod_rua) are foreign
keys into a consumer table (Consumidor). It turns out that the "Rua"
table has many unused records which I'd like to wipe out. For instance,
there are some 2800 unused records in the "Rua" table, and only some
200 records actually being used by the "Consumidor" table (which,
itself, has some 5000 records).

Attempting to find the unused records, I issued the following query:

a)

SELECT COD_RUA FROM RUA
WHERE COD_RUA NOT IN (
SELECT COD_RUA FROM CONSUMIDOR
)

To my surprise, the query came out empty. But the following query
showed the 200 or so records which *are* being used:

b)

SELECT COD_RUA FROM RUA
WHERE COD_RUA IN (
SELECT COD_RUA FROM CONSUMIDOR
)

I've found two solutions for the query to list the records *not
existing* in the Consumidor table:

c)

SELECT COD_RUA FROM RUA
WHERE COD_RUA NOT IN (
SELECT COD_RUA FROM CONSUMIDOR
WHERE COD_RUA IS NOT NULL
)

d)

SELECT COD_RUA FROM RUA
WHERE COD_RUA NOT IN (
SELECT COD_RUA FROM RUA
WHERE COD_RUA IN (
SELECT COD_RUA FROM CONSUMIDOR
)
)

I know that there are many other possible solutions to the query
(including left joins), but what I don't understand is why the query a)
failed.

Can some of you, oh mighty gurus, enlighten me?

For the record, here's how both tables are (partially) declared:

CREATE TABLE Rua (
Cod_Rua int NOT NULL ,
Rua varchar (35) NULL ,
-- ...
-- other unrelated fields
-- ...

CONSTRAINT Pk_CodRua
PRIMARY KEY (Cod_Rua)
)
CREATE TABLE Consumidor (
Cod_Consumidor int NOT NULL ,
Cod_Rua int NULL ,
-- ...
-- other unrelated fields
-- ...

CONSTRAINT Pk_CodConsumidor
PRIMARY KEY(Cod_Consumidor) ,

CONSTRAINT Fk_CodRua_Consumidor
FOREIGN KEY (Cod_Rua)
REFERENCES Rua (Cod_Rua)
)

Regards,

Branco Medeiros

Jul 10 '06 #1
3 2109
On 10 Jul 2006 15:29:37 -0700, Branco Medeiros wrote:

(snip)
>Attempting to find the unused records, I issued the following query:

a)

SELECT COD_RUA FROM RUA
WHERE COD_RUA NOT IN (
SELECT COD_RUA FROM CONSUMIDOR
)

To my surprise, the query came out empty. But the following query
showed the 200 or so records which *are* being used:
(snip)
>I know that there are many other possible solutions to the query
(including left joins), but what I don't understand is why the query a)
failed.

Can some of you, oh mighty gurus, enlighten me?
Hi Branco,

As you alreadyy found out, this has to do with rows in CONSUMIDOR that
have COD_RUA equal to NULL.

Suppose that CONSUMIDOR has only foru rows, with COD_RUA equal to 13,
27, 90 and NULL. In that case, the above query with NOT IN is equivalent
to this query:

SELECT COD_RUA FROM RUA
WHERE COD_RUA NOT IN (13, 27, 90, NULL)

And this, in turn, is equivalent to

SELECT COD_RUA FROM RUA
WHERE COD_RUA <13
AND COD_RUA <27
AND COD_RUA <90
AND COD_RUA <NULL

Now, any comparisoon of any value against NULL is always the "third"
boolan value in three-valued logic: Unknown. Since True AND Unknown
evaluates to Unknown, and False AND Unknown evaluates to False, the
WHERE clause above can only ever evaluate to Unknown or False, but never
to True - and the only rows included in the result of the SELECT
statement are those for which the WHERE clause is True! That''s whhy
you'll never see any rows in the output from this query.

Best practice: use IN and NOT IN only wiith a list of literals. Never
use either of them with a subquery. Apart from the problem with NULLs,
there are other pproblems: they often don't perform well on SQL Server,
and you can't extend the syntax for multi-column comparisons.

Every [NOT] IN with a subquery can always be transformed to a [NOT]
EXISTS with a subquery - and [NOT] EXISTS has no problems with NULL
values in the subquery, supports multi-column comparisons and usually
performs as good or better as [NOT] IN.

SELECT r.COD_RUA
FROM RUA AS r
WHERE NOT EXISTS
(SELECT *
FROM CONSUMIDOR AS c
WHERE c.COD_RUA = r.COD_RUA)
--
Hugo Kornelis, SQL Server MVP
Jul 10 '06 #2
Hugo Kornelis wrote:
<snip>
Best practice: use IN and NOT IN only wiith a list of literals. Never
use either of them with a subquery. Apart from the problem with NULLs,
there are other pproblems: they often don't perform well on SQL Server,
and you can't extend the syntax for multi-column comparisons.

Every [NOT] IN with a subquery can always be transformed to a [NOT]
EXISTS with a subquery - and [NOT] EXISTS has no problems with NULL
values in the subquery, supports multi-column comparisons and usually
performs as good or better as [NOT] IN.

SELECT r.COD_RUA
FROM RUA AS r
WHERE NOT EXISTS
(SELECT *
FROM CONSUMIDOR AS c
WHERE c.COD_RUA = r.COD_RUA)

Thanks, Hugo!

That was fast and *extremely* cool.

As a side note, I really missed an idiom to verify the (non) existence
of items accross tables and it seems that the syntax of [NOT] EXISTS
that you suggest is exactly what I needed.

Thanks a lot.

Best regards,

Branco.

Jul 10 '06 #3
Another suggestion: Change the DDL to make the column NOT NULL. This
is "mop the floor *and* fix the leak" philosophy.

Jul 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ken | last post by:
How can I get this line to work? I am trying to get the results entered in a form to be part of my query. I am able to get the form results but I get a error stating Syntax error (missing operator)...
4
by: emily_g107 | last post by:
Hi, I need to limit results in the following query type: ...
5
by: deko | last post by:
After developing an MDB in Access 2003 on WS03, then making it into an MDE and deploying it on a WinXP box with Access 2003 installed, I get this error: Function is not available in expressions...
9
by: Frederik | last post by:
Hi all, I'm building a C# application that uses a M$ Acces database. In one of the queries I use something like the following: SELECT id FROM mytable WHERE id IN (20, 12, 21, 14) The result...
11
by: Bruce Lawrence | last post by:
Ok, I'm baffled... I'm making a query in access 97 between 2 tables. There is a field in both tables called "DWGNO". OPENORD has a record with a DWGNO of "00000012345" DIEDATA has a record...
2
by: Curten | last post by:
Hi, I want to read data from a txt-file that looks like this: aaa 4 12.45 bbb 3 7.34 ccc 12 3.45 and store the data in an array of structures. The struct and array are defined...
6
by: KiwiGenie | last post by:
Hi..I am trying to make a search form. I am fairly new to access and could well be looking at it completely wrong. I have an unbound form with textboxes in the header for entering different search...
22
by: Alexandre Proulx | last post by:
I am trying to find 2 at the power of 601, but my calculator doesn't have a large enough screen, so I decided to make a program for my computer to do this, but when I get to a high number the...
1
by: Roger Trask | last post by:
Web Server: IIS6 PHP Version: 5.2.5 Database: MS SQLEXPRESS 2005 Loaded PDO Drivers: MSSQL, MySQL, ODBC When executing the following script ($dbName, $username, $pw, $myDSN have been changed in...
16
by: dougmeece | last post by:
Good day everyone, I have a database with 2 main forms. The first form is used to add records to the database and contains a command button that opens the 2nd form for records searching. On...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.