Solved

SQL Question

Posted on 2013-01-14
3
299 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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