If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > difference of two queries

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-14-09, 09:46
khdani khdani is offline
Registered User
 
Join Date: Nov 2008
Posts: 20
difference of two queries

hello,
i don't understand why those two queries don't give the same
result:
1)
Code:
select name 
from customer 
where custid in
(select custid
from ord
where ordid in (
	(select ordid
	from item,product
	where item.prodid = product.prodid
	and product.descrip like '%TENNIS%')
	minus
	(select ordid
	from item,product
	where item.prodid = product.prodid
	and product.descrip not like '%TENNIS%')
));
2)
Code:
select name
from customer
where custid in
	(select custid 
	from ord
	where ordid in (
		(select ordid
		from item,product
		where item.prodid = product.prodid
		and product.descrip like '%TENNIS%')))
minus
select name
from customer
where custid in
	(select custid 
	from ord
	where ordid in (
		(select ordid
		from item,product
		where item.prodid = product.prodid
		and product.descrip not like '%TENNIS%')));
they should give the same result but they don't....
Reply With Quote
  #2 (permalink)  
Old 11-14-09, 10:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
that's right, they don't

nor should they

perhaps you can explain why you think they should
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-14-09, 10:26
khdani khdani is offline
Registered User
 
Join Date: Nov 2008
Posts: 20
in (1) this subquery
Code:
(select ordid
	from item,product
	where item.prodid = product.prodid
	and product.descrip like '%TENNIS%')
	minus
	(select ordid
	from item,product
	where item.prodid = product.prodid
	and product.descrip not like '%TENNIS%')
should return ordid of those ordids who ordered items with desctiption which contains the word 'TENNIS' minus those with description which does not contains the word 'TENNIS'
and get the name of customers with those ordids.

in (2) i first get the names who ordered items with desctiption which contains the word
'TENNIS' minus names who ordered items with description which does not contains the word 'TENNIS'.

isn't the same ?
Reply With Quote
  #4 (permalink)  
Old 11-14-09, 10:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
query 1 returns customers who have any orders for ~only~ tennis items

query 2 returns customers who never ordered any non-tennis items


look at this sample data --

customer AAA order #123 contains only a tennis racquet
customer AAA order #456 contains a tennis racquet and a football

customer BBB order #789 contains a tennis racquet and tennis shorts
customer BBB order #555 contains tennis balls

query 1 returns both AAA and BBB

query 2 returns only BBB
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-14-09, 10:43
khdani khdani is offline
Registered User
 
Join Date: Nov 2008
Posts: 20
i think i got it,
thank you for explaining
Reply With Quote
  #6 (permalink)  
Old 11-14-09, 10:44
khdani khdani is offline
Registered User
 
Join Date: Nov 2008
Posts: 20
i think i got it,
thank you for explaining
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On