473,545 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Equivalence of join and exists

Hello all,

We are seeing strange behaviour for queries on a table we need to
convert data from. We try to find out whether table A(B_CONV_ID)
contains data that does not exists in table B(CONV_ID). We expect that
there is data missing in table B, thus finding a positive number of
records.

There are two tables, A and B.

A: ID, ..., B_CONV_ID
B: ID, ..., CONV_ID

The queries we are trying to execute and their results are the
following:

select count(*)
from A as AA
join B as BB on BB.CONV_ID = AA.B_CONV_ID
where BB.CONV_ID is null;

Result = 0;

select count(*)
from A as AA
where not exists (select 1 from B as BB where BB.CONV_ID =
AA.B_CONV_ID);

Result = 6273

A colleague confirmed me that the two queries were supposed to
retrieve the same result. Can anyone give us a clue of why the result
can be this different.
Nov 20 '07 #1
2 2240
On Nov 20, 3:50 pm, frederikenge... @gmail.com wrote:
Hello all,

We are seeing strange behaviour for queries on a table we need to
convert data from. We try to find out whether table A(B_CONV_ID)
contains data that does not exists in table B(CONV_ID). We expect that
there is data missing in table B, thus finding a positive number of
records.

There are two tables, A and B.

A: ID, ..., B_CONV_ID
B: ID, ..., CONV_ID

The queries we are trying to execute and their results are the
following:

select count(*)
from A as AA
join B as BB on BB.CONV_ID = AA.B_CONV_ID
where BB.CONV_ID is null;

Result = 0;

select count(*)
from A as AA
where not exists (select 1 from B as BB where BB.CONV_ID =
AA.B_CONV_ID);

Result = 6273

A colleague confirmed me that the two queries were supposed to
retrieve the same result. Can anyone give us a clue of why the result
can be this different.
You need to do an outer join. I assume A join B is the same A inner
join B, and in that case:

from A as AA join B as BB
on BB.CONV_ID = AA.B_CONV_ID
where BB.CONV_ID is null;

is a contradiction. Try:

select count(*)
from A as AA
left outer join B as BB
on BB.CONV_ID = AA.B_CONV_ID
where BB.CONV_ID is null;

/Lennart
Nov 20 '07 #2
fr************* @gmail.com wrote:
Hello all,

We are seeing strange behaviour for queries on a table we need to
convert data from. We try to find out whether table A(B_CONV_ID)
contains data that does not exists in table B(CONV_ID). We expect that
there is data missing in table B, thus finding a positive number of
records.

There are two tables, A and B.

A: ID, ..., B_CONV_ID
B: ID, ..., CONV_ID

The queries we are trying to execute and their results are the
following:

select count(*)
from A as AA
join B as BB on BB.CONV_ID = AA.B_CONV_ID
where BB.CONV_ID is null;

Result = 0;

select count(*)
from A as AA
where not exists (select 1 from B as BB where BB.CONV_ID =
AA.B_CONV_ID);

Result = 6273

A colleague confirmed me that the two queries were supposed to
retrieve the same result. Can anyone give us a clue of why the result
can be this different.
The queries do not have the same semantics, especially if you consider
NULLs. What happens is this:

The join predicate of the first query will evaluate to "unknown" if either
BB.CONV_ID or AA.B_CONV_ID happen to be NULL. "unknown" is interpreted
like "false", so all joined rows where either of the attributes is NULL
will be discarded. Adding "WHERE bb.conv_id IS NULL" doesn't change
anything at all because the relevant rows where already discarded before.

In the second query, you have pretty much the same. Your subselect does not
identify any qualifying rows in table B. But now you have a NOT EXISTS,
which leads to a "true" result for the predicate, and that qualifies the
row in table A.
What you can do to fix that is to correct the join condition in query 1 like
this:

ON ( bb.conv_id = aa.b_conv_id OR
bb.conv_id IS NULL OR
aa.b_conv_id IS NULL )

or maybe this, depending on what you want to achieve:

ON ( bb.conv_id = aa.b_conv_id OR
( bb.conv_id IS NULL AND
aa.b_conv_id IS NULL ) )

(You wouldn't need any of the parenthesis, but it makes things much
clearer.)

Alternatively, you can add "OR bb.conv_id IS NULL" to the WHERE clause in
the subselect of the 2nd query.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Nov 20 '07 #3

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

Similar topics

8
2051
by: red | last post by:
This is a simple join but I am having trouble with it. I feel like such an idiot. I start with this select statement, which works fine: $query = "SELECT a.auction, a.winner, b.id FROM PHPAUCTIONXL_winners a, PHPAUCTIONXL_auctions b WHERE a.auction=b.id"; Now I only want the results from auctions with ids that are not listed
41
3498
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes.
10
2252
by: Xah Lee | last post by:
another functional exercise with lists. Here's the perl documentation. I'll post a perl and the translated python version in 48 hours. =pod parti(aList, equalFunc) given a list aList of n elements, we want to return a list that is a
7
1671
by: Greg | last post by:
I'm a quantitative securities analyst working with Compustat data (company fiscal reports and pricing feeds). My coworker came across a problem that we fixed, but I'd like to understand 'why' it was happening and just don't get it yet. Here's the starting query (reduced to simple prefixes): ----INITIAL-----
3
1417
by: MaxPenguin | last post by:
Hi All, I am banging my head against a brick wall over this problem, so any help in the correct direction would be muchly appreciated! I have 2 SQL (MS SQL) server tables, realated to - a Property,
2
2027
by: Karsten Hilbert | last post by:
Dear all, for some reason I just cannot get my brain wrapped around the required syntax for the following. I think I need to either use a join or subselect(s): Situation: ---------- I have two tables (simplified here) for an international medical office application (www.gnumed.org):
5
4930
by: Alejandrina | last post by:
Hi all, I wonder if anyone can help with this sql: Table "Property_Name" catalogs properties: (property_id, property_name) Table "File_Property" catalogs files and their properties: (file_id, property_id, property_value) Data example:
0
2275
by: yeahuh | last post by:
Quick and dirty version. Godaddy server using MySQL 4.0.24 I’m trying a left join to obtain id’s in table A(cars) that are NOT in car_id in table B(newspaper): *This is a cut down version to simplify testing. Full version is posted towards the end. SELECT C.id FROM cars C LEFT OUTER JOIN newspaper N USING (C.id=N.car_id) WHERE...
2
10661
by: hema118 | last post by:
Hi.. Have a tricky question here. Your help is appreciated ! I have 3 tables as below TABLE A DEPT_NBR MGT_NBR MGR_NAME 1 111 ABD
0
7659
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7811
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5975
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5334
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1887
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 we have to send another system
1
1019
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
709
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.