473,508 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"NOT IN" statement not working in MS Access?

Hi all,
Here is my dilemma.

I have two tables, one called HS DATA and one called
ImportedHighSchools.

Both tables contain an ID code for each record.

The table ImportedHighSchools contains 554 records.

I have tried to make a query that would find all of the IDs in
ImportedHighSchools that do not appear in HS DATA. Here it is.

SELECT * FROM ImportedHighSchools WHERE ImportedHighSchools.ID NOT IN
(SELECT [HS DATA].ID FROM HS DATA)

The problem is, it doesn't return any records. And I have put in fake
IDs in table ImportedHighSchools.

But when I run the same query, just without the NOT statement, it
returns 501 records. Would it not make sense then, that if I had the
NOT IN statement in the query, it would return the remaining 53
records? I can't seem to figure this one out.
Any insight would be very helpful.

Thanks
Josh

Nov 13 '05 #1
9 68368
ka*****@appliedtech.us wrote:
I have two tables, one called HS DATA and one called
ImportedHighSchools.

Both tables contain an ID code for each record.

The table ImportedHighSchools contains 554 records.

I have tried to make a query that would find all of the IDs in
ImportedHighSchools that do not appear in HS DATA. Here it is.

SELECT * FROM ImportedHighSchools WHERE ImportedHighSchools.ID NOT IN
(SELECT [HS DATA].ID FROM HS DATA)


You need an outer join between the tables on ID such that all records from
ImportedHighSchools are returned and those in HS DATA will show as null.

HTH - Keith.
www.keithwilby.com
Nov 13 '05 #2
ka*****@appliedtech.us wrote:
I have tried to make a query that would find all of the IDs in
ImportedHighSchools that do not appear in HS DATA. Here it is.


You can use the Unmatched Query wizard for the same function. It uses a
LEFT JOIN for the purpose.

That doesn't answer the original question, I know. Are both the ID
fields of the same datatype? What result do you get with the LEFT JOIN
strategy?
--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #3
ka*****@appliedtech.us wrote:
Hi all,
Here is my dilemma.

I have two tables, one called HS DATA and one called
ImportedHighSchools.

Both tables contain an ID code for each record.

The table ImportedHighSchools contains 554 records.

I have tried to make a query that would find all of the IDs in
ImportedHighSchools that do not appear in HS DATA. Here it is.

SELECT * FROM ImportedHighSchools WHERE ImportedHighSchools.ID NOT IN
(SELECT [HS DATA].ID FROM HS DATA)

The problem is, it doesn't return any records. And I have put in fake
IDs in table ImportedHighSchools.

But when I run the same query, just without the NOT statement, it
returns 501 records. Would it not make sense then, that if I had the
NOT IN statement in the query, it would return the remaining 53
records? I can't seem to figure this one out.
Any insight would be very helpful.

Thanks
Josh


Go to Queries/New and try the wizard for Find Unmatched. Or...

Queries/New/Design. Select HSData and ImportedHighSchools. Press Close.

Now drag a relationship line between the two ID fields. Now dlb-Click
on the line and set the relation to All ImportedHighSchools and only
those in HSData that match.

Now drag the ID fields from both tables. In the criteria row for the ID
in HSData enter
Is Null

That should do it. Run it and see.

BTW, you had a code line
(SELECT [HS DATA].ID FROM HS DATA)
and it probably should be
(SELECT [HS DATA].ID FROM [HS DATA])

I will say this. Sub-selects like yours are notoriously slow when you
have lots of records.
Nov 13 '05 #4
Both the IDs are of the same datatype. Is there a reason why the IN
statement works, but the NOT IN statement doesn't?

Thanks!

Nov 13 '05 #5
Hey guys! The wizard in Access did end up working! I never thought I
would ever use an Access wizard. Turns out, it worked out great,
although I would have never come up with that SQL. Can you guys give
me some insight into why I needed the LEFT JOIN and the the IS NULL?
Thanks
Josh

Nov 13 '05 #6
Hey guys! The wizard in Access did end up working! I never thought I
would ever use an Access wizard. Turns out, it worked out great,
although I would have never come up with that SQL. Can you guys give
me some insight into why I needed the LEFT JOIN and the the IS NULL?
Thanks
Josh

Nov 13 '05 #7
ka*****@appliedtech.us wrote:
Both the IDs are of the same datatype. Is there a reason why the IN
statement works, but the NOT IN statement doesn't?


Do you have Nulls in either table?

--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #8
No, just some blank records.

Nov 13 '05 #9
A "regular" inner join selects rows from the two tables where each table has
row(s) than have matching data in the join field. Say you have a list of
clients, where one client can have many invoices, but not all clients have
been issued invoices. A query like this

SELECT Clients.[Company Name], Invoices.[Invoice Number]
FROM Clients INNER JOIN Invoices ON Clients.[Client ID] = Invoices.Client;

selects clients and their matching invoices, but only those clients that
have had invoices issued.

A left join, like this

SELECT Clients.[Company Name], Invoices.[Invoice Number]
FROM Clients LEFT JOIN Invoices ON Clients.[Client ID] = Invoices.Client;

selects all clients, whether they have invoices associated with them or not,
and of course, the invoice number if they do.

A right join, like this

SELECT Clients.[Company Name], Invoices.[Invoice Number]
FROM Clients RIGHT JOIN Invoices ON Clients.[Client ID] = Invoices.Client;

does the converse: selects all invoices, even if they don't have clients
associated. These are usually orphaned records that need to be either need
to be deleted or correctly matched with existing clients.

However, don't overlook Salad's point regarding your original subquery:
subquerys do work, and sometimes (especially in deletion or update
situations) they do things that unmatched queries can't. I think the problem
with your subquery was that your table name consisted of two words ("HS
Data") and for the query to identify the table correctly, the two-word name
needs to be enclosed in brackets: [HS Data]
<ka*****@appliedtech.us> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Hey guys! The wizard in Access did end up working! I never thought I
would ever use an Access wizard. Turns out, it worked out great,
although I would have never come up with that SQL. Can you guys give
me some insight into why I needed the LEFT JOIN and the the IS NULL?
Thanks
Josh

Nov 13 '05 #10

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

Similar topics

3
58162
by: B. Harwood | last post by:
This is probably a simple question, but I'm stumped. I cannot figure out a way to do the following. For example, say I have two tables - one of used toys and one of broken toys. Say I wanted to...
2
2052
by: mantas44 | last post by:
Hello all, I have two tables tab_a id(int) tab_b id(int)
9
19445
by: Bob Alston | last post by:
I have a drop down combo box that gives the user to enter an item not in the list by adding it to the list. The list is a table. It works fine on Access2003 but fails on Access2002/XP. ON XP, it...
3
2113
by: Branco Medeiros | last post by:
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...
6
2406
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...
13
3807
by: ThePrinceIsRight | last post by:
I have a problem with using a subquery in MS Access. The purpose of the sub-query is to create a list of people who have had doctor exams in the past 6 months and exclude them from the main query....
2
2040
by: MohamedSaleh | last post by:
HI all I have the following problem Table SOURCE has columns <char COL1, integar COL2> and has rows {A,1}, {A,2,}, {,A,3}, {B,1}, {B,2},, {C,1},
1
1308
by: kevinmccarthy | last post by:
Hello, I'm trying to compose a sql statement that will select some columns from a parent table and get the child records that DO NOT contain a certain value. From a simple perspective I have...
11
1998
by: bcurtu | last post by:
Hi, I have a BIIIIIG problem with the next query: cursor.execute(""" SELECT titem.object_id, titem.tag_id FROM tagging_taggeditem titem WHERE titem.object_id IN (%s) """,( eid_list))
0
7233
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
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7410
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...
1
7067
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
7505
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
5650
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
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.