Solved

SQL Question

Posted on 2013-01-14
3
300 Views
Last Modified: 2013-01-16
Which orders contain widgets?
      Columns to display: ORDERS.orderid, ORDERS.salesdate
      The word 'widget' may not be the only word in the part description (use a wildcard).

so without using exist I get 14 records with this query:

SELECT Orders.orderid, Orders.salesdate
FROM  Inventory INNER JOIN
               OrderItems ON Inventory.partid = OrderItems.partid INNER JOIN
               Orders ON OrderItems.orderid = Orders.orderid
WHERE (Inventory.description LIKE 'WIDGET%')

But when I use EXISTS  with the query below  I get 21 records    why?

SELECT  Orders.orderid, Orders.salesdate
FROM  Inventory INNER JOIN
               OrderItems ON Inventory.partid = OrderItems.partid INNER JOIN
               Orders ON OrderItems.orderid = Orders.orderid
WHERE EXISTS
                   (SELECT description
                    FROM   Inventory AS Inventory_1
                    WHERE (description LIKE 'WIDGET%'))
GROUP BY Orders.orderid, Orders.salesdate
ORDER BY Orders.salesdate
0
Comment
Question by:ocdc
3 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38777109
Exists will consider the NULL descriptions also some extra rows
I think better to use first query
here you will get some details
http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
0
 

Author Comment

by:ocdc
ID: 38779130
my question is   How can I get the same result by using EXISTS as the first one which gives 14 records? Thanks
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 38780754
The second EXISTS will always be true, so it does not screen out any orders.

Btw,  you need to use '%WIDGET%' for a full wildcard effect.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question