Solved

SQL Question

Posted on 2013-01-14
3
298 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:
ScottPletcher 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Error message when scheduling a job using a linked Server 12 50
How to enforce inte 8 32
SQL Query 2 56
Need help with a query 6 55
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now