?
Solved

SQL Question

Posted on 2013-01-14
3
Medium Priority
?
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

Accepted Solution

by:
Scott Pletcher earned 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

719 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