dbTalk Databases Forums  

Puzzled by this SQL?

comp.databases.oracle.server comp.databases.oracle.server


Discuss Puzzled by this SQL? in the comp.databases.oracle.server forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Adrian Harrison
 
Posts: n/a

Default Puzzled by this SQL? - 08-12-2011 , 05:00 AM






"asset" table contains 163 rows
"asset_attr_value" table contains 200 rows (but only 19 rows match
where clause below)

This SQL using EXISTS returns 163 rows -

SELECT asset_id
FROM asset
WHERE
EXISTS (SELECT asset_id
FROM asset_attr_value av
WHERE ( av.clasfn_schm_key = 'base'
AND av.attr_key =
'test_attr_base_1'
AND av.value_text = 'a1' )
AND av.record_deleted = 'N')
ORDER BY asset_id


But this one using IN returns 19 -

SELECT asset_id
FROM asset
WHERE
asset_id IN (SELECT asset_id
FROM asset_attr_value av
WHERE ( av.clasfn_schm_key = 'base'
AND av.attr_key =
'test_attr_base_1'
AND av.value_text = 'a1' )
AND av.record_deleted = 'N')
ORDER BY asset_id

The IN is correct but why does the 1st return all, surely this should
return 19 also?

thanks in advance

Reply With Quote
  #2  
Old   
Adrian Harrison
 
Posts: n/a

Default Re: Puzzled by this SQL? - 08-12-2011 , 06:05 AM






On Aug 12, 11:19*am, "Preston" <dontwant... (AT) nowhere (DOT) invalid> wrote:
Quote:
Adrian Harrison wrote:
"asset" table contains 163 rows
"asset_attr_value" table contains 200 rows (but only 19 rows match
where clause below)

This SQL using EXISTS returns 163 rows -

SELECT asset_id
FROM * asset
WHERE
* * * *EXISTS (SELECT asset_id
* * * * * * * * * * * * * FROM * asset_attr_value av
* * * * * * * * * * * * * WHERE *( av.clasfn_schm_key = 'base'
* * * * * * * * * * * * * * * * * *AND av.attr_key =
'test_attr_base_1'
* * * * * * * * * * * * * * * * * *AND av.value_text = 'a1' )
* * * * * * * * * * * * * * * * *AND av.record_deleted = 'N')
ORDER *BY asset_id

But this one using IN returns 19 -

SELECT asset_id
FROM * asset
WHERE
* * * *asset_id IN (SELECT asset_id
* * * * * * * * * * * * * FROM * asset_attr_value av
* * * * * * * * * * * * * WHERE *( av.clasfn_schm_key = 'base'
* * * * * * * * * * * * * * * * * *AND av.attr_key =
'test_attr_base_1'
* * * * * * * * * * * * * * * * * *AND av.value_text = 'a1' )
* * * * * * * * * * * * * * * * *AND av.record_deleted = 'N')
ORDER *BY asset_id

The IN is correct but why does the 1st return all, surely this should
return 19 also?

Think about what you're missing; how does the outer query know when
there's a matching asset_id in the sub-query?

--
Preston.- Hide quoted text -

- Show quoted text -
ah ha!

SELECT a.asset_id
FROM asset a
WHERE
EXISTS (SELECT av.asset_id
FROM asset_attr_value av
WHERE ( av.clasfn_schm_key = 'base'
AND av.attr_key =
'test_attr_base_1'
AND av.value_text = 'a1' )
AND av.record_deleted = 'N' and
a.asset_id = av.asset_id)
ORDER BY a.asset_id

thanks for that, is there any advantage is using IN over EXISTS in
this case?

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.